My Local Development Environment on NixOS
First published at Tuesday 21 January 2025
My Local Development Environment on NixOS
Probably irrelevant to most, but still logging this. How do I set up my local development environment while using NixOS? For different projects I require different tools, which I do not always have installled globally. In the past I was using the nix-shell
command manually but now I automated it.
I was always using nix-shell -p <packages> --command fish
to install all packages required for a current project. I usually document those dependencies in the README.md, while I only have the most basic tools installed globally. Context: I (still) always develop using vim in my terminal (fish), so any IDE automations won't work for me.
It still got a little bit tedious, so I decided to automate this process. I create a .dev.env listing the packages I need, for example, for a current playground project:
rustc cargo rustfmt
And then I added the following function to my ~/.config/fish/config.fish
. Adding it ~/.config/fish/functions/<function>.fish
didn't work, since the autoloading behavior seemingly is only triggered when I start typing the function name, while I want this to always happen when entering the working directory of a certain project:
function __check_dev_env --on-variable PWD --description "Switch into development environment, if available."
status --is-command-substitution; and return
# Exit if we're already in a nix-shell
if test -n "$IN_NIX_SHELL"
return
end
if test -f ".dev.env"
set PKG_CONTENTS (cat .dev.env)
echo "Starting development environment with $PKG_CONTENTS"
echo
NIXPKGS_ALLOW_UNFREE=1 nix-shell -p (string split " " $PKG_CONTENTS) --command fish
end
end
This function is now executed every time I change my current working directory (more exactly, when the $PWD
variable changes) and checks for a .dev.env
file. If one is present and we'ren't already in a nix-shell those packages will be installed.
NixOS will take care of clearing stuff up afterwards, so I'll never have to bother about uninstalling those dependencies or any left-overs later.
Subscribe to updates
There are multiple ways to stay updated with new posts on my blog: