aboutsummaryrefslogtreecommitdiff
path: root/content/notes/2023-06-25-development-environments-with-nix.md
diff options
context:
space:
mode:
Diffstat (limited to 'content/notes/2023-06-25-development-environments-with-nix.md')
-rw-r--r--content/notes/2023-06-25-development-environments-with-nix.md68
1 files changed, 68 insertions, 0 deletions
diff --git a/content/notes/2023-06-25-development-environments-with-nix.md b/content/notes/2023-06-25-development-environments-with-nix.md
new file mode 100644
index 0000000..6bae302
--- /dev/null
+++ b/content/notes/2023-06-25-development-environments-with-nix.md
@@ -0,0 +1,68 @@
1---
2title: "Development environments with Nix"
3url: development-environments-with-nix.html
4date: 2023-06-25T16:38:10+02:00
5type: note
6draft: false
7tags: [random]
8---
9
10Nix is amazing for making reproducible cross OS development environment.
11
12First you need to [install Nix package
13manager](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
29And then run it `nix-shell`. By default it will look for `shell.nix` file. If
30you want to specify a different file use `nix-shell file.nix`. That is about it.
31
32When the shell is spawned it could happen that your `PS1` prompt will be
33overwritten and your prompt will look differently. In that case you need to
34either 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
36to `1`.
37
38I also have a modified `PS1` prompt for Bash that I use and it also catches the
39usage of Nix shell.
40
41```sh
42NIX_SHELL_PRESERVE_PROMPT=1
43
44parse_git_branch() {
45 git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/ (\1)/'
46}
47
48is_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
55export 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
58And this is what it looks like when you are in a Nix shell. Otherwise that part
59of prompt is omitted
60
61![PS1 Prompt](/notes/ps1-prompt.png)
62
63More 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/