diff options
| author | Mitja Felicijan <m@mitjafelicijan.com> | 2023-07-12 18:35:08 +0200 |
|---|---|---|
| committer | Mitja Felicijan <m@mitjafelicijan.com> | 2023-07-12 18:35:08 +0200 |
| commit | 23a56bd50b04211da3cab45f72c3390711b2416b (patch) | |
| tree | ab9a4a0136b4cce06dba7d853e296f682f807dbb /content/notes/development-environments-with-nix.md | |
| parent | cecb4b48a39a3558979b9c4b50e45bf605a3684e (diff) | |
| download | mitjafelicijan.com-23a56bd50b04211da3cab45f72c3390711b2416b.tar.gz | |
Moved notes and posts into subfolders
Diffstat (limited to 'content/notes/development-environments-with-nix.md')
| -rw-r--r-- | content/notes/development-environments-with-nix.md | 68 |
1 files changed, 68 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..6bae302 --- /dev/null +++ b/content/notes/development-environments-with-nix.md | |||
| @@ -0,0 +1,68 @@ | |||
| 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: note | ||
| 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 | ||
| 13 | manager](https://nixos.org/download.html). | ||
| 14 | |||
| 15 | - Create a file `shell.nix` in your project folder. | ||
| 16 | - In the section that has `python3` etc add programs you want to use. These can | ||
| 17 | be CLI or GUI applications. It doesn't matter to Nix. | ||
| 18 | |||
| 19 | ```nix | ||
| 20 | { pkgs ? import <nixpkgs> {} }: | ||
| 21 | pkgs.mkShell { | ||
| 22 | nativeBuildInputs = with pkgs.buildPackages; [ | ||
| 23 | python3 | ||
| 24 | tinycc | ||
| 25 | ]; | ||
| 26 | } | ||
| 27 | ``` | ||
| 28 | |||
| 29 | And then run it `nix-shell`. By default it will look for `shell.nix` file. If | ||
| 30 | you want to specify a different file use `nix-shell file.nix`. That is about it. | ||
| 31 | |||
| 32 | When the shell is spawned it could happen that your `PS1` prompt will be | ||
| 33 | overwritten and your prompt will look differently. In that case you need to | ||
| 34 | either do `NIX_SHELL_PRESERVE_PROMPT=1 nix shell` or add | ||
| 35 | `NIX_SHELL_PRESERVE_PROMPT` variable to your `bashrc` or `zshrc` file and set it | ||
| 36 | to `1`. | ||
| 37 | |||
| 38 | I also have a modified `PS1` prompt for Bash that I use and it also catches the | ||
| 39 | usage of Nix shell. | ||
| 40 | |||
| 41 | ```sh | ||
| 42 | NIX_SHELL_PRESERVE_PROMPT=1 | ||
| 43 | |||
| 44 | parse_git_branch() { | ||
| 45 | git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/ (\1)/' | ||
| 46 | } | ||
| 47 | |||
| 48 | is_inside_nix_shell() { | ||
| 49 | nix_shell_name="$(basename "$IN_NIX_SHELL" 2>/dev/null)" | ||
| 50 | if [[ -n "$nix_shell_name" ]]; then | ||
| 51 | echo " \e[0;36m(nix-shell)\e[0m" | ||
| 52 | fi | ||
| 53 | } | ||
| 54 | |||
| 55 | 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$ " | ||
| 56 | ``` | ||
| 57 | |||
| 58 | And this is what it looks like when you are in a Nix shell. Otherwise that part | ||
| 59 | of prompt is omitted | ||
| 60 | |||
| 61 |  | ||
| 62 | |||
| 63 | More resources: | ||
| 64 | |||
| 65 | - https://nixos.wiki/wiki/Development_environment_with_nix-shell | ||
| 66 | - https://nixos.wiki/wiki/Main_Page | ||
| 67 | - https://itsfoss.com/why-use-nixos/ | ||
| 68 | - https://mynixos.com/ | ||
