Development environments with Nix

note, Jun 25, 2023, on Mitja Felicijan's blog

Nix is amazing for making reproducible cross OS development environment.

First you need to install Nix package manager.

{ pkgs ? import <nixpkgs> {} }:
  pkgs.mkShell {
    nativeBuildInputs = with pkgs.buildPackages; [
	  python3
	  tinycc
	];
}

And then run it nix-shell. By default it will look for shell.nix file. If you want to specify a different file use nix-shell file.nix. That is about it.

When the shell is spawned it could happen that your PS1 prompt will be overwritten and your prompt will look differently. In that case you need to either do NIX_SHELL_PRESERVE_PROMPT=1 nix shell or add NIX_SHELL_PRESERVE_PROMPT variable to your bashrc or zshrc file and set it to 1.

I also have a modified PS1 prompt for Bash that I use and it also catches the usage of Nix shell.

NIX_SHELL_PRESERVE_PROMPT=1

parse_git_branch() {
	git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/ (\1)/'
}

is_inside_nix_shell() {
	nix_shell_name="$(basename "$IN_NIX_SHELL" 2>/dev/null)"
	if [[ -n "$nix_shell_name" ]]; then
		echo " \e[0;36m(nix-shell)\e[0m"
	fi
}

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$ "

And this is what it looks like when you are in a Nix shell. Otherwise that part of prompt is omitted

PS1 Prompt

More resources:

Other notes

DateTitle
Aerial photography of algae spotted on river Sava
Edsger W. Dijkstra Manuscripts ebook
Online radio streaming with MPV from terminal
Write and read structs to/from files in Cc
Making cgit look nicer
Embedding resources into binary with Cc
Using ffmpeg to combine videos side by side
Add lazy loading of images in Jekyll posts
Change Alt key to Win key under Xfce4
Use option key as meta in Alacritty under macOS
Alacritty open links with modifier
Run 9front in Qemuplan9
Extending dte editor
Mount Plan9 over networkplan9
Display xterm color palette