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