diff options
| -rw-r--r-- | content/notes/development-environments-with-nix.md | 67 | ||||
| -rw-r--r-- | static/notes/ps1-prompt.png | bin | 0 -> 24272 bytes |
2 files changed, 67 insertions, 0 deletions
diff --git a/content/notes/development-environments-with-nix.md b/content/notes/development-environments-with-nix.md new file mode 100644 index 0000000..a6769ed --- /dev/null +++ b/content/notes/development-environments-with-nix.md | |||
| @@ -0,0 +1,67 @@ | |||
| 1 | --- | ||
| 2 | title: "Development environments with Nix" | ||
| 3 | url: development-environments-with-nix.html | ||
| 4 | date: 2023-06-25T16:38:10+02:00 | ||
| 5 | type: notes | ||
| 6 | draft: false | ||
| 7 | tags: [random] | ||
| 8 | --- | ||
| 9 | |||
| 10 | Nix is amazing for making reproducible cross OS development environment. | ||
| 11 | |||
| 12 | First you need to [install Nix package manager](https://nixos.org/download.html). | ||
| 13 | |||
| 14 | - Create a file `shell.nix` in your project folder. | ||
| 15 | - In the section that has `python3` etc add programs you want to use. These can | ||
| 16 | be CLI or GUI applications. It doesn't matter to Nix. | ||
| 17 | |||
| 18 | ```nix | ||
| 19 | { pkgs ? import <nixpkgs> {} }: | ||
| 20 | pkgs.mkShell { | ||
| 21 | nativeBuildInputs = with pkgs.buildPackages; [ | ||
| 22 | python3 | ||
| 23 | tinycc | ||
| 24 | ]; | ||
| 25 | } | ||
| 26 | ``` | ||
| 27 | |||
| 28 | And then run it `nix-shell`. By default it will look for `shell.nix` file. If | ||
| 29 | you want to specify a different file use `nix-shell file.nix`. That is about it. | ||
| 30 | |||
| 31 | When the shell is spawned it could happen that your `PS1` prompt will be | ||
| 32 | overwritten and your prompt will look differently. In that case you need to | ||
| 33 | either do `NIX_SHELL_PRESERVE_PROMPT=1 nix shell` or add | ||
| 34 | `NIX_SHELL_PRESERVE_PROMPT` variable to your `bashrc` or `zshrc` file and set it | ||
| 35 | to `1`. | ||
| 36 | |||
| 37 | I also have a modified `PS1` prompt for Bash that I use and it also catches the | ||
| 38 | usage of Nix shell. | ||
| 39 | |||
| 40 | ```sh | ||
| 41 | NIX_SHELL_PRESERVE_PROMPT=1 | ||
| 42 | |||
| 43 | parse_git_branch() { | ||
| 44 | git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/ (\1)/' | ||
| 45 | } | ||
| 46 | |||
| 47 | is_inside_nix_shell() { | ||
| 48 | nix_shell_name="$(basename "$IN_NIX_SHELL" 2>/dev/null)" | ||
| 49 | if [[ -n "$nix_shell_name" ]]; then | ||
| 50 | echo " \e[0;36m(nix-shell)\e[0m" | ||
| 51 | fi | ||
| 52 | } | ||
| 53 | |||
| 54 | export PS1="[\033[38;5;9m\]\u@\h\[$(tput sgr0)\]]$(is_inside_nix_shell)\[\033[33m\]\$(parse_git_branch)\[\033[00m\] \w\[$(tput sgr0)\] \n$ " | ||
| 55 | ``` | ||
| 56 | |||
| 57 | And this is what it looks like when you are in a Nix shell. Otherwise that part | ||
| 58 | of prompt is omitted | ||
| 59 | |||
| 60 |  | ||
| 61 | |||
| 62 | More resources: | ||
| 63 | |||
| 64 | - https://nixos.wiki/wiki/Development_environment_with_nix-shell | ||
| 65 | - https://nixos.wiki/wiki/Main_Page | ||
| 66 | - https://itsfoss.com/why-use-nixos/ | ||
| 67 | - https://mynixos.com/ | ||
diff --git a/static/notes/ps1-prompt.png b/static/notes/ps1-prompt.png new file mode 100644 index 0000000..e27c714 --- /dev/null +++ b/static/notes/ps1-prompt.png | |||
| Binary files differ | |||
