diff options
| author | Collin Williams <96917990+bluedragon1221@users.noreply.github.com> | 2026-08-26 15:07:01 -0500 |
|---|---|---|
| committer | Collin Williams <96917990+bluedragon1221@users.noreply.github.com> | 2026-08-26 15:07:01 -0500 |
| commit | f409013733c6499dcc271c17fb33bd71ff6bb5e2 (patch) | |
| tree | 4021f526b70c34d40f73a0a569bf51a4a12a1230 | |
| parent | c1c50bf3b20449c26f36f1fd0968b9559e256bd6 (diff) | |
new article: A solution to slow nix-shells
| -rw-r--r-- | devshell.html | 129 | ||||
| -rw-r--r-- | writing.html | 1 |
2 files changed, 130 insertions, 0 deletions
diff --git a/devshell.html b/devshell.html new file mode 100644 index 0000000..59b36af --- /dev/null +++ b/devshell.html @@ -0,0 +1,129 @@ +<!DOCTYPE html> +<html> + <head> + <title>A solution to slow nix-shells</title> + <meta property="article:published_time" content="2026-08-26"> + <script src="./article.js"></script> + </head> + + <body> + <article> + <header></header> + + <p> + Tired of waiting for <code>nix-shell</code> to re-download <code>github:NixOS/nixpkgs</code> after you <code>nix-collect-garbage</code> (or just randomly sometimes)? + Me too! + That's why I went down this rabit hole of making an instant nix shell. + </p> + + <div id="toc"></div> + + <p> + First, let's clarify what I mean by a nix shell. + I'm simply referring to the idea of hiding the packages that you use for development behind a command, and managing those packages using nix. + Most people are familiar with this through the <code>nix-shell</code> or <code>nix shell</code> commands. + (For non-nix people, yes those are in fact two very different things) + </p> + <p> + My issue with how <code>nix shell</code> is that it has to redownload your packages if they're not in the nix store (one cause of this would be if you recently ran <code>nix-collect-garbage</code>), and it has to re-evaluate the correct environment variables and packages every time you launch the shell. + These two things together mean that every time you run <code>nix shell</code>, you'll be waiting before you can access those packages. + And, personally, I'd rather spend my time waiting for my code to compile, not waiting to have access to my compiler. + </p> + + <h2 id="existing-solutions">Existing solutions</h2> + <p> + There's a lot of existing solutions to this problem, but none of them do exactly what I want. + </p> + <p> + I used to be a big fan of <a href="https://github.com/xzfc/cached-nix-shell"><code>cached-nix-shell</code></a>. + It's cool because it (as the name suggests) caches your nix shell, so the next time you start it up, the launch time is instant (it removes the second factor of waiting mentioned in the previous section). + But for some reason it breaks for certain packages that rely on paths that might not exist anymore. + For example, I had issues with <code>rustc</code> not working when launched through <code>cached-nix-shell</code> if it was using a cached version. + </p> + <p> + Another solution that I think too many people fall for is to just install the packages into your system <code>$PATH</code> with <code>environment.systemPackages</code>. + One of the great things about NixOS is that you don't have to resort to this! + On Arch, if you install a package with <code>pacman</code>, it has to go into your system <code>$PATH</code>. + But on NixOS, we don't have to bloat the system path with our developer dependencies. + This is <em>exactly why</em> <code>nix-shell</code> exists in the first place! + </p> + <p> + The most hardcore-nix-nerd option here is to manually mark your nix-shell as a GC root. + This tells <code>nix-collect-garbage</code> to not remove the path or anything that it depends on, preserving your nix-shells through garbage collection. + Maybe some day I'll write my own tool that wraps this functionality to provide GC-protected nix shells, but honestly that just sounds like too much work. + (for more information about how Garbage Collection works on Nix, check out <a href="https://nixos.org/guides/nix-pills/11-garbage-collector">the Nix Pill about the Garbage Collector</a>) + </p> + <p> + There's probably a million other ways that people solve this problem that I'm not familiar with. + But, I don't really care, since I already found the perfect solution for my personal use case, which I'll explain now. + </p> + + <h2 id="devshell-packages">Devshell packages</h2> + <p> + Here's the solution I came up! + In your NixOS config, using home-manager or <a href="https://github.com/feel-co/hjem">hjem</a> or whatever, just build a package that launches your shell but with some extra programs. + </p> + + <pre><code>{pkgs, ...}: let + rust-shell = let + rustProfile = pkgs.buildEnv { + name = "rust-profile"; + paths = with pkgs; [rustc cargo rust-analyzer]; + }; + in + pkgs.writeShellScriptBin "rust-shell" '' + export PATH="${rustProfile}/bin:$PATH" + exec $SHELL + ''; + + py-shell = let + pythonProfile = pkgs.python314.withPackages (ps: + with ps; [ + numpy + matplotlib + pandas + jupyter + ipython + ]); + in + pkgs.writeShellScriptBin "py-shell" '' + export PATH="${pythonProfile}/bin:$PATH" + exec $SHELL + ''; +in { + packages = [rust-shell py-shell]; +}</code></pre> + + <h2 id="thoughts">Thoughts</h2> + <p> + This approach has good parts and bad parts. + For one, it's instant. No more waiting for nix to redownload the latest version of nixpkgs and copy new paths into your system store. + And, just like nix-shell, it doesn't bloat your system <code>$PATH</code>. + The packages in the subshell are not available in the host system, just like nix-shell. + </p> + <p> + But there are some downsides as well. + </p> + <p> + The biggest one is that you can't add new packages to your devshells without rebuilding your system. + The extent to which is an issue depends on the language and how you manage libraries. + For Rust, this is not an issue. + Most people that use rust use cargo to build their software, which automatically manages dependencies per-project. + The same goes for serverside javascript with <code>node_modules</code>. + But if you prefer to use nix to manage your dependencies (like with Python, for example), this might not be the best idea. + </p> + <p> + Additionally, the versions of the packages will be tied to the version of nixpkgs that you used the last time your rebuilt your system. + This shouldn't be an issue for people like me who are constantly tweaking and rebuilding stuff. + </p> + <p> + Also, this can make your system closure really large. + In my examples, the Rust one isn't that bad, but the Python one pulls in hundreds of python dependencies into the system closure. + This can make rebuilds longer (when the packages need to update) and take up more space on your computer. + </p> + <p> + Even with these trade-offs, I'm willing to pay the price to have instantaneous access to my developer dependencies! + </p> + </article> + </body> +</html> diff --git a/writing.html b/writing.html index f1b8d93..cec4bc1 100644 --- a/writing.html +++ b/writing.html @@ -19,6 +19,7 @@ <article class="markdown-body"> <h1>My Writing</h1> <ul> + <li><a href="./devshell">A solution to slow nix-shells</a></li> <li><a href="./net">Minimal Laptop Networking with NixOS</a></li> <li><a href="./reversible">Reversible Programming is cool (Reversible Part 1)</a></li> <li><a href="./fib">I accidentally derived a formula for the Fibonacci Sequence</a></li> |
