diff options
Diffstat (limited to 'devshell.html')
| -rw-r--r-- | devshell.html | 98 |
1 files changed, 59 insertions, 39 deletions
diff --git a/devshell.html b/devshell.html index 59b36af..899e862 100644 --- a/devshell.html +++ b/devshell.html @@ -1,32 +1,51 @@ <!DOCTYPE html> <html> <head> + <meta charset="UTF-8"> + <meta name="viewport" content="width=device-width, initial-scale=1.0"> + <link rel="icon" href="static/Logo.svg" type="image/svg+xml"> + <link rel="stylesheet" href="article.css"> + <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> + <article class="markdown-body"> + <header> + <div class="header-top"> + <a href="writing.html" class="header-logo-link"></a> + <div class="header-info"> + <h1>A solution to slow nix-shells</h1> + <time datetime="2026-08-26">Published Aug 8, 2026</time> + </div> + </div> + <hr> + </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)? + Tired of waiting for <code><kw>nix-shell</kw></code> to re-download <code><str>github:NixOS/nixpkgs</str></code> after you <code><kw>nix-collect-garbage</kw></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> + <h2>Table of Contents</h2> + <ul> + <li><a href="#intro">What's wrong with nix-shell?</a></li> + <li><a href="#existing-solutions">Existing solutions</a></li> + <li><a href="#devshell-packages">Devshell packages</a></li> + <li><a href="#thoughts">Thoughts</a></li> + </ul> + <h2 id="intro">What's wrong with nix-shell?</h2> <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. + Most people are familiar with this through the <code><kw>nix-shell</kw></code> or <code><kw>nix</kw> <prop>shell</prop></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. + My issue with how <code><kw>nix</kw> <prop>shell</prop></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><kw>nix-collect-garbage</kw></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><kw>nix</kw> <prop>shell</prop></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> @@ -35,21 +54,21 @@ 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>. + I used to be a big fan of <a href="https://github.com/xzfc/cached-nix-shell"><code><kw>cached-nix-shell</kw></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. + For example, I had issues with <code><kw>rustc</kw></code> not working when launched through <code><kw>cached-nix-shell</kw></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>. + Another solution that I think too many people fall for is to just install the packages into your system <code>$PATH</code> with <code><prop>environment</prop>.<prop>systemPackages</prop></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>. + On Arch, if you install a package with <code><kw>pacman</kw></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! + This is <em>exactly why</em> <code><kw>nix-shell</kw></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. + The most hardcore-nix-nerd option here is to manually mark your devshell as a GC root. + This tells <code><kw>nix-collect-garbage</kw></code> to not remove the path or anything that it depends on, preserving your devshells 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> @@ -64,34 +83,34 @@ 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]; + <pre><code>{<prop>pkgs</prop>, ...}: <kw>let</kw> + <prop>rust-shell</prop> = <kw>let</kw> + <prop>rustProfile</prop> = <prop>pkgs</prop>.<prop>buildEnv</prop> { + <prop>name</prop> = <str>"rust-profile"</str>; + <prop>paths</prop> = <kw>with</kw> <prop>pkgs</prop>; [<prop>rustc</prop> <prop>cargo</prop> <prop>rust-analyzer</prop> <prop>gcc</prop>]; }; - in - pkgs.writeShellScriptBin "rust-shell" '' - export PATH="${rustProfile}/bin:$PATH" + <kw>in</kw> + <prop>pkgs</prop>.<prop>writeShellScriptBin</prop> <str>"rust-shell"</str> <str>'' + export PATH="${<prop>rustProfile</prop>}/bin:$PATH" exec $SHELL - ''; + ''</str>; - py-shell = let - pythonProfile = pkgs.python314.withPackages (ps: - with ps; [ - numpy - matplotlib - pandas - jupyter - ipython + <prop>py-shell</prop> = <kw>let</kw> + <prop>pythonProfile</prop> = <prop>pkgs</prop>.<prop>python314</prop>.<prop>withPackages</prop> (<prop>ps</prop>: + <kw>with</kw> <prop>ps</prop>; [ + <prop>numpy</prop> + <prop>matplotlib</prop> + <prop>pandas</prop> + <prop>jupyter</prop> + <prop>ipython</prop> ]); - in - pkgs.writeShellScriptBin "py-shell" '' - export PATH="${pythonProfile}/bin:$PATH" + <kw>in</kw> + <prop>pkgs</prop>.<prop>writeShellScriptBin</prop> <str>"py-shell"</str> <str>'' + export PATH="${<prop>pythonProfile</prop>}/bin:$PATH" exec $SHELL - ''; -in { - packages = [rust-shell py-shell]; + ''</str>; +<kw>in</kw> { + <prop>packages</prop> = [<prop>rust-shell</prop> <prop>py-shell</prop>]; }</code></pre> <h2 id="thoughts">Thoughts</h2> @@ -121,6 +140,7 @@ in { 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> + <hr> <p> Even with these trade-offs, I'm willing to pay the price to have instantaneous access to my developer dependencies! </p> |
