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