aboutsummaryrefslogtreecommitdiff
path: root/content/notes/2023-06-25-development-environments-with-nix.md
diff options
context:
space:
mode:
authorMitja Felicijan <mitja.felicijan@gmail.com>2024-03-10 14:59:14 +0100
committerMitja Felicijan <mitja.felicijan@gmail.com>2024-03-10 14:59:14 +0100
commit1100562e29f6476448b656dbddd4cf22505523f6 (patch)
tree442eec492199104bd49dfd74474ce89ade8fcac9 /content/notes/2023-06-25-development-environments-with-nix.md
parenta40d80be378e46a6c490e1b99b0d8f4acd968503 (diff)
downloadmitjafelicijan.com-1100562e29f6476448b656dbddd4cf22505523f6.tar.gz
Move back to JBMAFP
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.md67
1 files changed, 67 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..8aa104c
--- /dev/null
+++ b/content/notes/2023-06-25-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: note
6draft: false
7---
8
9Nix is amazing for making reproducible cross OS development environment.
10
11First you need to [install Nix package
12manager](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](/assets/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/