myWebsite

Built with Hugo and a modified version of the risotto theme.
Log | Files | Refs

index.html (19159B)


      1 <!DOCTYPE html>
      2 <html lang="en">
      3 
      4     <head><script src="/livereload.js?mindelay=10&amp;v=2&amp;port=1313&amp;path=livereload" data-no-instant defer></script><title>Nix Flakes &ndash; Fedor_Vinogradov</title>
      5 <meta name="description" content="I prefer matrix over anything.">
      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/projects/" title="">Projects</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. I&rsquo;ve been using them for the best part of a year now, and I can&rsquo;t imagine my life without them. Sadly, due to poor documentation, and their &ldquo;experimental&rdquo; 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></blockquote>
     64 <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>
     65 <h2 id="initial-setup">Initial Setup</h2>
     66 <p>Make sure you have nix installed, that is being either NixOS or a stand alone nix installation.</p>
     67 <h3 id="enable-flakes-in-nixos">Enable Flakes in NixOS</h3>
     68 <p>To enable flakes in NixOS, add the following to your configuration.nix file.</p>
     69 <pre tabindex="0"><code>nix.settings.experimental-features = [ &#34;nix-command&#34; &#34;flakes&#34; ];
     70 </code></pre><h3 id="enable-flakes-in-stand-alone-nix">Enable Flakes in Stand-Alone Nix</h3>
     71 <p>To enable flakes in a stand alone nix installation, add the following to your nix.conf file.</p>
     72 <pre tabindex="0"><code>experimental-features = nix-command flakes
     73 </code></pre><h2 id="running-a-nix-flake">Running a Nix Flake</h2>
     74 <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>
     75 <h3 id="running-a-local-flakes">Running a local flakes</h3>
     76 <p>To run a flake that is in the current directory, you can use the following command:</p>
     77 <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
     78 </span></span></code></pre></div><p>Where &ldquo;output&rdquo; is the name of the output that you want to run, an example of this will be shown in the next section.</p>
     79 <p>If you want to run the default output, you can omit the output name:</p>
     80 <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 .
     81 </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>
     82 <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
     83 </span></span></code></pre></div><h3 id="running-a-flake-from-a-git-repository">Running a flake from a git repository</h3>
     84 <p>To run a flake from a git repository, you can use the following command:</p>
     85 <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
     86 </span></span></code></pre></div><p>if a flake is located in a subdir, you can use the following syntax:</p>
     87 <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
     88 </span></span></code></pre></div><blockquote>
     89 <p><strong>Note:</strong> &ldquo;github&rdquo; can be replaced by any other git provider, such as &ldquo;gitlab&rdquo; or &ldquo;sourcehut&rdquo;.</p></blockquote>
     90 <h2 id="creating-a-flake">Creating a Flake</h2>
     91 <p>It&rsquo;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>
     92 <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>{
     93 </span></span><span style="display:flex;"><span>  description <span style="color:#f92672">=</span> <span style="color:#e6db74">&#34;My flake&#34;</span>;
     94 </span></span><span style="display:flex;"><span>  inputs <span style="color:#f92672">=</span> {
     95 </span></span><span style="display:flex;"><span>    nixpkgs<span style="color:#f92672">.</span>url <span style="color:#f92672">=</span> <span style="color:#e6db74">&#34;github:nixos/nixpkgs/nixos-unstable&#34;</span>;
     96 </span></span><span style="display:flex;"><span>  };
     97 </span></span><span style="display:flex;"><span>  outputs <span style="color:#f92672">=</span> { self<span style="color:#f92672">,</span> nixpkgs }: {
     98 </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;
     99 </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;
    100 </span></span><span style="display:flex;"><span>  };
    101 </span></span><span style="display:flex;"><span>}
    102 </span></span></code></pre></div><p>Let&rsquo;s quickly go over what all of that means.</p>
    103 <p><strong>description:</strong> -&gt; just a description of the flake, doesnt don anything besides that</p>
    104 <p><strong>inputs:</strong> as explained above, contains links to places from which the packages will be grabed.</p>
    105 <p><strong>outputs</strong> needs a bigger explanation:</p>
    106 <ul>
    107 <li>
    108 <p><code>{ self, nixpkgs }</code> tells which inputs to use to make the outputs</p>
    109 </li>
    110 <li>
    111 <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>
    112 </li>
    113 <li>
    114 <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>
    115 </li>
    116 <li>
    117 <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>
    118 </li>
    119 </ul>
    120 <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>
    121 <p>You probably noticed the wired syntax with the use of &ldquo;self&rdquo; 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>
    122 <blockquote>
    123 <p><strong>Note:</strong> &ldquo;self&rdquo; syntax is quite rare, there is no need to understand it well.</p></blockquote>
    124 <h3 id="preparing-your-flake">Preparing Your Flake</h3>
    125 <p>lets get rid of all the repeating code and make some variables to make the flake easier to work with.</p>
    126 <p>To set variables, we will use the <code>let ... in {}</code> syntax.</p>
    127 <p>Here is a template that I use to make all of my nix flakes:</p>
    128 <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>
    129 </span></span><span style="display:flex;"><span>{
    130 </span></span><span style="display:flex;"><span>   description <span style="color:#f92672">=</span> <span style="color:#e6db74">&#34;A very basic flake&#34;</span>;
    131 </span></span><span style="display:flex;"><span>
    132 </span></span><span style="display:flex;"><span>   inputs <span style="color:#f92672">=</span> {
    133 </span></span><span style="display:flex;"><span>     nixpkgs<span style="color:#f92672">.</span>url <span style="color:#f92672">=</span> <span style="color:#e6db74">&#34;github:nixos/nixpkgs?ref=nixos-unstable&#34;</span>; }; outputs <span style="color:#f92672">=</span> { self<span style="color:#f92672">,</span> nixpkgs }:
    134 </span></span><span style="display:flex;"><span>   <span style="color:#66d9ef">let</span>
    135 </span></span><span style="display:flex;"><span>     system <span style="color:#f92672">=</span> <span style="color:#e6db74">&#34;x86_64-linux&#34;</span>;
    136 </span></span><span style="display:flex;"><span>     pkgs <span style="color:#f92672">=</span> nixpkgs<span style="color:#f92672">.</span>legacyPackages<span style="color:#f92672">.</span><span style="color:#e6db74">${</span>system<span style="color:#e6db74">}</span>; <span style="color:#75715e"># use when you wnat only FOSS packages</span>
    137 </span></span><span style="display:flex;"><span>     <span style="color:#75715e"># use when you want to allow unfree packages:</span>
    138 </span></span><span style="display:flex;"><span>     <span style="color:#75715e"># pkgs = import nixpkgs { system = &#34;x86_64-linux&#34;; config.allowUnfree = true; };</span>
    139 </span></span><span style="display:flex;"><span>   <span style="color:#66d9ef">in</span>
    140 </span></span><span style="display:flex;"><span>   {
    141 </span></span><span style="display:flex;"><span>     <span style="color:#75715e"># you nix code goes here, here are some examples:</span>
    142 </span></span><span style="display:flex;"><span>
    143 </span></span><span style="display:flex;"><span>     packages<span style="color:#f92672">.</span><span style="color:#e6db74">${</span>system<span style="color:#e6db74">}</span><span style="color:#f92672">.</span>default <span style="color:#f92672">=</span> pkgs<span style="color:#f92672">.</span>hello;
    144 </span></span><span style="display:flex;"><span>
    145 </span></span><span style="display:flex;"><span>     devShells<span style="color:#f92672">.</span><span style="color:#e6db74">${</span>system<span style="color:#e6db74">}</span><span style="color:#f92672">.</span>default <span style="color:#f92672">=</span> 
    146 </span></span><span style="display:flex;"><span>       pkgs<span style="color:#f92672">.</span>mkShell {
    147 </span></span><span style="display:flex;"><span>         buildInputs <span style="color:#f92672">=</span> <span style="color:#66d9ef">with</span> pkgs; [
    148 </span></span><span style="display:flex;"><span>          hello
    149 </span></span><span style="display:flex;"><span>         ];  
    150 </span></span><span style="display:flex;"><span>       };
    151 </span></span><span style="display:flex;"><span>     };
    152 </span></span><span style="display:flex;"><span>}
    153 </span></span></code></pre></div><p>You can just copy the following flake and run it with either <code>nix run</code> or <code>nix shell</code>. First will run just the GNU Hello package and the 2nd will make a shell where you can run GNU Hello.</p>
    154 <h2 id="use-cases">Use Cases</h2>
    155 <h3 id="managing-nixos-configurationnix">Managing NixOS configuration.nix</h3>
    156 <p>You can wrap you Nixos configuration into a flake, which will give you much more control over your inputs. and allow the configuration to be more portable.</p>
    157 <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>{
    158 </span></span><span style="display:flex;"><span>  description <span style="color:#f92672">=</span> <span style="color:#e6db74">&#34;My NixOS configuration&#34;</span>;
    159 </span></span><span style="display:flex;"><span>  inputs <span style="color:#f92672">=</span> {
    160 </span></span><span style="display:flex;"><span>    nixpkgs<span style="color:#f92672">.</span>url <span style="color:#f92672">=</span> <span style="color:#e6db74">&#34;github:nixos/nixpkgs/nixos-unstable&#34;</span>;
    161 </span></span><span style="display:flex;"><span>  };
    162 </span></span><span style="display:flex;"><span>  outputs <span style="color:#f92672">=</span> { self<span style="color:#f92672">,</span> nixpkgs<span style="color:#f92672">,</span> <span style="color:#f92672">...</span> }: {
    163 </span></span><span style="display:flex;"><span>    nixosConfigurations<span style="color:#f92672">.</span>my-host <span style="color:#f92672">=</span> nixpkgs<span style="color:#f92672">.</span>lib<span style="color:#f92672">.</span>nixosSystem {
    164 </span></span><span style="display:flex;"><span>      system <span style="color:#f92672">=</span> <span style="color:#e6db74">&#34;x86_64-linux&#34;</span>;
    165 </span></span><span style="display:flex;"><span>      modules <span style="color:#f92672">=</span> [
    166 </span></span><span style="display:flex;"><span>        <span style="color:#e6db74">./configuration.nix</span>
    167 </span></span><span style="display:flex;"><span>      ];
    168 </span></span><span style="display:flex;"><span>    };
    169 </span></span><span style="display:flex;"><span>  };
    170 </span></span><span style="display:flex;"><span>}
    171 </span></span></code></pre></div><h3 id="making-development-environment">Making development environment</h3>
    172 <p>I wont go into detail on how to make a developemnt shell. I am in the process of making another article about that. All I will show is that you can wrap your shell into a flake, which will give it way way more protable and predicatble.</p>
    173     </div>
    174     <footer class="content__footer"></footer>
    175 
    176             </section>
    177 
    178             <section class="page__aside">
    179                 <div class="aside__about">
    180 <div class="aside__about">
    181     
    182     
    183 <h1 class="about__title">My Socials</h1>
    184 <p class="about__description">I prefer matrix over anything.</p>
    185 </div>
    186 
    187 
    188 <ul class="aside__social-links">
    189     
    190     <li>
    191         <a href="" rel="me" aria-label="SourceHut" title="SourceHut"><i class="fa-regular fa-circle" aria-hidden="true"></i></a>&nbsp;
    192     </li>
    193     
    194     <li>
    195         <a href="https://matrix.to/#/@fedorvin:matrix.org" rel="me" aria-label="Matrix" title="Matrix"><i class="fa-regular fa-comment" aria-hidden="true"></i></a>&nbsp;
    196     </li>
    197     
    198     <li>
    199         <a href="/index.xml" rel="me" aria-label="RSS" title="RSS"><i class="fa-solid fa-rss" aria-hidden="true"></i></a>&nbsp;
    200     </li>
    201     
    202     <li>
    203         <a href="mailto:vino-f@pm.me" rel="me" aria-label="Email" title="Email"><i class="fa-regular fa-envelope" aria-hidden="true"></i></a>&nbsp;
    204     </li>
    205     
    206     <li>
    207         <a href="https://linkedin.com/in/fedor-vinogradov-b4454219a" rel="me" aria-label="LinkedIn" title="LinkedIn"><i class="fa-brands fa-linkedin-in" aria-hidden="true"></i></a>&nbsp;
    208     </li>
    209     
    210 </ul>
    211 </div>
    212                 <hr>
    213                 <div class="aside__content">
    214     <p>A simple guide to nix flakes</p>
    215     
    216 
    217     
    218 
    219 		    <hr>
    220 		    <nav id="TableOfContents">
    221   <ul>
    222     <li><a href="#what-are-nix-flakes">What Are Nix Flakes</a></li>
    223     <li><a href="#initial-setup">Initial Setup</a>
    224       <ul>
    225         <li><a href="#enable-flakes-in-nixos">Enable Flakes in NixOS</a></li>
    226         <li><a href="#enable-flakes-in-stand-alone-nix">Enable Flakes in Stand-Alone Nix</a></li>
    227       </ul>
    228     </li>
    229     <li><a href="#running-a-nix-flake">Running a Nix Flake</a>
    230       <ul>
    231         <li><a href="#running-a-local-flakes">Running a local flakes</a></li>
    232         <li><a href="#running-a-flake-from-a-git-repository">Running a flake from a git repository</a></li>
    233       </ul>
    234     </li>
    235     <li><a href="#creating-a-flake">Creating a Flake</a>
    236       <ul>
    237         <li><a href="#preparing-your-flake">Preparing Your Flake</a></li>
    238       </ul>
    239     </li>
    240     <li><a href="#use-cases">Use Cases</a>
    241       <ul>
    242         <li><a href="#managing-nixos-configurationnix">Managing NixOS configuration.nix</a></li>
    243         <li><a href="#making-development-environment">Making development environment</a></li>
    244       </ul>
    245     </li>
    246   </ul>
    247 </nav>
    248 		    <hr>
    249                 </div>
    250             </section>
    251 
    252             <footer class="page__footer">
    253 <p class="copyright"></p>
    254 </footer>
    255 
    256         </div>
    257     </body>
    258 
    259 </html>