index.html (13267B)
1 <!DOCTYPE html> 2 <html lang="en"> 3 4 <head><script src="/livereload.js?mindelay=10&v=2&port=1313&path=livereload" data-no-instant defer></script><title>Nix Flakes – Fedor_Vinogradov</title> 5 <meta name="description" content="SoruceHut"> 6 7 <meta name="viewport" content="width=device-width, initial-scale=1"> 8 <meta charset="UTF-8"/> 9 10 11 12 <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.6.0/css/all.min.css" integrity="sha512-Kc323vGBEqzTmouAECnVceyQqyqdsSiqLQISBL29aUW4U/M7pSPA/gEUZQqv1cwx4OnYxTxve5UMg5GT6L4JJg==" crossorigin="anonymous" referrerpolicy="no-referrer" /> 13 14 15 <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/academicons/1.9.4/css/academicons.min.css" integrity="sha512-IW0nhlW5MgNydsXJO40En2EoCkTTjZhI3yuODrZIc8cQ4h1XcF53PsqDHa09NqnkXuIe0Oiyyj171BqZFwISBw==" crossorigin="anonymous" referrerpolicy="no-referrer" /> 16 17 18 <link rel="stylesheet" href="http://localhost:1313/css/palettes/nord-dark.css"> 19 <link rel="stylesheet" href="http://localhost:1313/css/risotto.css"> 20 <link rel="stylesheet" href="http://localhost:1313/css/custom.css"> 21 22 23 24 25 26 27 28 29 30 31 </head> 32 33 <body> 34 <div class="page"> 35 36 <header class="page__header"><nav class="page__nav main-nav"> 37 <ul> 38 <li class="nomarker"><h1 class="page__logo"><a href="http://localhost:1313/" class="page__logo-inner">Fedor_Vinogradov</a></h1></li> 39 40 41 <li class="main-nav__item"><a class="nav-main-item" href="http://localhost:1313/about/" title="">About</a></li> 42 43 <li class="main-nav__item"><a class="nav-main-item" href="http://localhost:1313/posts/" title="">Posts</a></li> 44 45 </ul> 46 </nav> 47 48 </header> 49 50 <section class="page__body"> 51 <header class="content__header"> 52 <h1>Nix Flakes</h1> 53 </header> 54 <div class="content__body"> 55 <p>Nix flakes are amazing. Ive been using them for the best part of a year now and I can’t imagine my life without them. Sadly, due to poor documentation, and their “experimental” status, they are not as widely adopted as they should be. In this post, I will write an easy-to-understand guide to nix flakes.</p> 56 <h2 id="what-are-nix-flakes">What Are Nix Flakes</h2> 57 <p>Nix flakes is the new way to define a nix environment that keeps it as declarative as possible.</p> 58 <p>A flake has inputs and outputs.</p> 59 <p><strong>inputs</strong> contain links to derivation (package) repositories that will be used to build the flake. For most use cases, you will only need the <a href="https://search.nixos.org/packages">nixpkgs</a> input, as it probably contains all the packages you will ever need.</p> 60 <p><strong>outputs</strong> are the actual configurations/shells/packages that the flake will produce when run.</p> 61 <p>After the flake is run for the first time, it will generate a lock file that contains the hashes of all the inputs, ensuring that the flake will always produce the same output if given the same inputs.</p> 62 <blockquote> 63 <p><strong>In a nutshell,</strong> if done correctly, you can make system configs and dev environments that will <strong>never break</strong> because of a dependency change or a system update.</p> 64 </blockquote> 65 <p>Even if, God forbid, you mess something up and a package stops working, going back to a working version is as simple as grabbing the previous lock file from git and running the flake again.</p> 66 <h2 id="initial-setup">Initial Setup</h2> 67 <p>Make sure you have nix installed, that is being either NixOS or a stand alone nix installation.</p> 68 <h3 id="enable-flakes-in-nixos">Enable Flakes in NixOS</h3> 69 <p>To enable flakes in NixOS, add the following to your configuration.nix file.</p> 70 <pre tabindex="0"><code>nix.settings.experimental-features = [ "nix-command" "flakes" ]; 71 </code></pre><h3 id="enable-flakes-in-stand-alone-nix">Enable Flakes in Stand-Alone Nix</h3> 72 <p>To enable flakes in a stand alone nix installation, add the following to your nix.conf file.</p> 73 <pre tabindex="0"><code>experimental-features = nix-command flakes 74 </code></pre><h1 id="running-a-nix-flake">Running a Nix Flake</h1> 75 <p>Due to the versatile nature of nix flakes, there are loads of ways to utilize them. Here I will list the ones that I use the most and that I think are the most useful.</p> 76 <h2 id="running-a-local-flakes">Running a local flakes</h2> 77 <p>To run a flake that is in the current directory, you can use the following command:</p> 78 <div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-bash" data-lang="bash"><span style="display:flex;"><span>nix run .#output 79 </span></span></code></pre></div><p>Where “output” is the name of the output that you want to run, an example of this will be shown in the next section.</p> 80 <p>If you want to run the default output, you can omit the output name:</p> 81 <div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-bash" data-lang="bash"><span style="display:flex;"><span>nix run . 82 </span></span></code></pre></div><p>running a shell follows the same syntax, but instead of <em>nix run</em>, you use <em>nix shell</em>:</p> 83 <div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-bash" data-lang="bash"><span style="display:flex;"><span>nix shell .#output 84 </span></span></code></pre></div><h2 id="running-a-flake-from-a-git-repository">Running a flake from a git repository</h2> 85 <p>To run a flake from a git repository, you can use the following command:</p> 86 <div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-bash" data-lang="bash"><span style="display:flex;"><span>nix run github:username/repo#output 87 </span></span></code></pre></div><p>if a flake is located in a subdir, you can use the following syntax:</p> 88 <div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-bash" data-lang="bash"><span style="display:flex;"><span>nix run github:username/repo?dir<span style="color:#f92672">=</span>dir/to/flake#output 89 </span></span></code></pre></div><blockquote> 90 <p><strong>Note:</strong> “github” can be replaced by any other git provider, such as “gitlab” or “sourcehut”.</p> 91 </blockquote> 92 <h1 id="creating-a-flake">Creating a Flake</h1> 93 <p>It’s as simple as running <code>nix flake init</code> in an empty directory. This will create a flake.nix file with the following content:</p> 94 <div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-nix" data-lang="nix"><span style="display:flex;"><span>{ 95 </span></span><span style="display:flex;"><span> description <span style="color:#f92672">=</span> <span style="color:#e6db74">"My flake"</span>; 96 </span></span><span style="display:flex;"><span> inputs <span style="color:#f92672">=</span> { 97 </span></span><span style="display:flex;"><span> nixpkgs<span style="color:#f92672">.</span>url <span style="color:#f92672">=</span> <span style="color:#e6db74">"github:nixos/nixpkgs/nixos-unstable"</span>; 98 </span></span><span style="display:flex;"><span> }; 99 </span></span><span style="display:flex;"><span> outputs <span style="color:#f92672">=</span> { self<span style="color:#f92672">,</span> nixpkgs }: { 100 </span></span><span style="display:flex;"><span> packages<span style="color:#f92672">.</span>x86_64-linux<span style="color:#f92672">.</span>hello <span style="color:#f92672">=</span> nixpkgs<span style="color:#f92672">.</span>legacyPackages<span style="color:#f92672">.</span>x86_64-linux<span style="color:#f92672">.</span>hello; 101 </span></span><span style="display:flex;"><span> packages<span style="color:#f92672">.</span>x86_64-linux<span style="color:#f92672">.</span>default <span style="color:#f92672">=</span> self<span style="color:#f92672">.</span>packages<span style="color:#f92672">.</span>x86_64-linux<span style="color:#f92672">.</span>hello; 102 </span></span><span style="display:flex;"><span> }; 103 </span></span><span style="display:flex;"><span>} 104 </span></span></code></pre></div><p>Let’s quickly go over what all of that means.</p> 105 <p><strong>description:</strong> -> just a description of the flake, doesnt don anything besides that</p> 106 <p><strong>inputs:</strong> as explained above, contains links to places from which the packages will be grabed.</p> 107 <p><strong>outputs</strong> needs a bigger explanation:</p> 108 <ul> 109 <li> 110 <p><code>{ self, nixpkgs }</code> tells which inputs to use to make the outputs</p> 111 </li> 112 <li> 113 <p><code>packages.x86_64-linux.hello</code> contains the nix code that will be run if the flake is called with <code> nix run .#hello</code> and will run the GNU hello package. (on x85_64-linux)</p> 114 </li> 115 <li> 116 <p><code>nixpkgs.legacyPackages.x86_64-linux</code> <strong>doesnt</strong> mean that the package is Legacy. Its just the syntax to speed some functions up.</p> 117 </li> 118 <li> 119 <p><code>packages.x86_64-linux.default</code> nix code then runs if an output is not specified: <code>nix run</code> or <code>nix run .#</code> (on x85_64-linux)</p> 120 </li> 121 </ul> 122 <p>The default flake has two outputs, one that runs GNU hello and the other that runs GNU hello by default. You can have as many outputs as you like. Forther down, I will show you how to make a flake that runs a dev shell and nixos configurations.</p> 123 <p>You probably noticed the wired syntax with the use of “self” input. Nix flakes can have other flakes as an input, even itself. Looking back at the flake, you can see that <code>packages.x86_64-linux.default</code> simply reffers back to the flake itself and calls <code>packages.x86_64-linux.hello</code> which then runs GNU hello.</p> 124 <blockquote> 125 <p><strong>Note:</strong> “self” syntax is quite rare, there is no need to understand it well.</p> 126 </blockquote> 127 <!-- raw HTML omitted --> 128 <!-- raw HTML omitted --> 129 <!-- raw HTML omitted --> 130 <!-- raw HTML omitted --> 131 <!-- raw HTML omitted --> 132 <!-- raw HTML omitted --> 133 <!-- raw HTML omitted --> 134 <!-- raw HTML omitted --> 135 <!-- raw HTML omitted --> 136 <!-- raw HTML omitted --> 137 <!-- raw HTML omitted --> 138 <!-- raw HTML omitted --> 139 <!-- raw HTML omitted --> 140 <!-- raw HTML omitted --> 141 <!-- raw HTML omitted --> 142 <!-- raw HTML omitted --> 143 <!-- raw HTML omitted --> 144 <!-- raw HTML omitted --> 145 <!-- raw HTML omitted --> 146 <!-- raw HTML omitted --> 147 <!-- raw HTML omitted --> 148 <!-- raw HTML omitted --> 149 <!-- raw HTML omitted --> 150 <!-- raw HTML omitted --> 151 <!-- raw HTML omitted --> 152 <!-- raw HTML omitted --> 153 <!-- raw HTML omitted --> 154 <!-- raw HTML omitted --> 155 <!-- raw HTML omitted --> 156 <!-- raw HTML omitted --> 157 <!-- raw HTML omitted --> 158 <!-- raw HTML omitted --> 159 <!-- raw HTML omitted --> 160 <!-- raw HTML omitted --> 161 <!-- raw HTML omitted --> 162 <!-- raw HTML omitted --> 163 <!-- raw HTML omitted --> 164 <!-- raw HTML omitted --> 165 <!-- raw HTML omitted --> 166 <!-- raw HTML omitted --> 167 <!-- raw HTML omitted --> 168 <!-- raw HTML omitted --> 169 <!-- raw HTML omitted --> 170 <!-- raw HTML omitted --> 171 <!-- raw HTML omitted --> 172 <!-- raw HTML omitted --> 173 <!-- raw HTML omitted --> 174 <!-- raw HTML omitted --> 175 <!-- raw HTML omitted --> 176 <!-- raw HTML omitted --> 177 <!-- raw HTML omitted --> 178 <!-- raw HTML omitted --> 179 <!-- raw HTML omitted --> 180 <!-- raw HTML omitted --> 181 <!-- raw HTML omitted --> 182 <!-- raw HTML omitted --> 183 <!-- raw HTML omitted --> 184 185 </div> 186 <footer class="content__footer"></footer> 187 188 </section> 189 190 <section class="page__aside"> 191 <div class="aside__about"> 192 <div class="aside__about"> 193 194 195 <h1 class="about__title">My Socials</h1> 196 <p class="about__description">SoruceHut</p> 197 </div> 198 199 200 <ul class="aside__social-links"> 201 202 <li> 203 <a href="" rel="me" aria-label="SourceHut" title="SourceHut"><i class="fa-regular fa-circle" aria-hidden="true"></i></a> 204 </li> 205 206 <li> 207 <a href="" rel="me" aria-label="Matrix" title="Matrix"><i class="fa-regular fa-comment" aria-hidden="true"></i></a> 208 </li> 209 210 <li> 211 <a href="" rel="me" aria-label="LinkedIn" title="LinkedIn"><i class="fa-brands fa-linkedin-in" aria-hidden="true"></i></a> 212 </li> 213 214 </ul> 215 </div> 216 <hr> 217 <div class="aside__content"> 218 <p>A simple guide to nix flakes</p> 219 220 <p> 221 222 2024-11-05 223 </p> 224 225 226 227 228 </div> 229 </section> 230 231 <footer class="page__footer"><p> 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 <br/><span class="active">$ echo $LANG<br/><b>English</b></span><br/> 250 251 252 253 254 255 256 257 </p> 258 <br /><br /> 259 <p class="copyright"></p> 260 <p class="advertisement">Powered by <a href="https://gohugo.io/">hugo</a> and <a href="https://github.com/joeroe/risotto">risotto</a>.</p> 261 </footer> 262 263 </div> 264 </body> 265 266 </html>