<?xml version="1.0" encoding="utf-8"?> 
<rss version="2.0">
 <channel>
  <title>XGQT's blog: Posts tagged 'nix'</title>
  <description>XGQT's blog: Posts tagged 'nix'</description>
  <link>https://xgqt.gitlab.io/blog/tags/nix.html</link>
  <lastBuildDate>Mon, 12 Feb 2024 20:28:31 UT</lastBuildDate>
  <pubDate>Mon, 12 Feb 2024 20:28:31 UT</pubDate>
  <ttl>1800</ttl>
  <item>
   <title>Safer Nix installation</title>
   <link>https://xgqt.gitlab.io/blog/posts/2024/02/12/safer-nix-installation/?utm_source=nix&amp;utm_medium=RSS</link>
   <guid isPermaLink="false">urn:https-xgqt-gitlab-io:-blog-posts-2024-02-12-safer-nix-installation</guid>
   <pubDate>Mon, 12 Feb 2024 20:28:31 UT</pubDate>
   <author>Maciej Barć</author>
   <description>
&lt;p&gt;Nix is useful for quickly testing out software and providing a strict environment that can be shared between people.&lt;/p&gt;

&lt;p&gt;Today I&amp;rsquo;m trying out Nix &lt;em&gt;again&lt;/em&gt;, this time I want to do it &lt;strong&gt;my way&lt;/strong&gt;.&lt;/p&gt;

&lt;h2 id="installation-process"&gt;Installation process&lt;/h2&gt;

&lt;h3 id="nix-store"&gt;Nix store&lt;/h3&gt;

&lt;p&gt;I know Nix needs &amp;ldquo;Nix store&amp;rdquo; installation on &lt;code&gt;/&lt;/code&gt; (the system root).&lt;/p&gt;

&lt;p&gt;Create it manually to prevent the installation script from calling &lt;code&gt;sudo&lt;/code&gt;. 1st I switch to the root account, and then I run:&lt;/p&gt;

&lt;div class="brush: shell"&gt;
 &lt;div class="source"&gt;
  &lt;table class="sourcetable"&gt;
   &lt;tbody&gt;
    &lt;tr&gt;
     &lt;td class="linenos"&gt;
      &lt;div class="linenodiv"&gt;
       &lt;pre&gt;&lt;span class="normal"&gt;1&lt;/span&gt;
&lt;span class="normal"&gt;2&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/td&gt;
     &lt;td class="code"&gt;
      &lt;div&gt;
       &lt;pre&gt;&lt;span&gt;&lt;/span&gt;mkdir&lt;span class="w"&gt; &lt;/span&gt;-p&lt;span class="w"&gt; &lt;/span&gt;-m&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="m"&gt;0755&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;/nix
chown&lt;span class="w"&gt; &lt;/span&gt;-R&lt;span class="w"&gt; &lt;/span&gt;xy:xy&lt;span class="w"&gt; &lt;/span&gt;/nix
&lt;/pre&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/div&gt;

&lt;/div&gt;

&lt;h3 id="running-the-install-script"&gt;Running the install script&lt;/h3&gt;

&lt;p&gt;Download the Nix install script and examine the contents.&lt;/p&gt;

&lt;div class="brush: shell"&gt;
 &lt;div class="source"&gt;
  &lt;table class="sourcetable"&gt;
   &lt;tbody&gt;
    &lt;tr&gt;
     &lt;td class="linenos"&gt;
      &lt;div class="linenodiv"&gt;
       &lt;pre&gt;&lt;span class="normal"&gt;1&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/td&gt;
     &lt;td class="code"&gt;
      &lt;div&gt;
       &lt;pre&gt;&lt;span&gt;&lt;/span&gt;curl&lt;span class="w"&gt; &lt;/span&gt;-L&lt;span class="w"&gt; &lt;/span&gt;https://nixos.org/nix/install&lt;span class="w"&gt; &lt;/span&gt;&amp;gt;&lt;span class="w"&gt; &lt;/span&gt;nix_install.sh
&lt;/pre&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/div&gt;

&lt;/div&gt;

&lt;p&gt;Then, run it with &lt;code&gt;--no-daemon&lt;/code&gt; to prevent it running as system service.&lt;/p&gt;

&lt;div class="brush: shell"&gt;
 &lt;div class="source"&gt;
  &lt;table class="sourcetable"&gt;
   &lt;tbody&gt;
    &lt;tr&gt;
     &lt;td class="linenos"&gt;
      &lt;div class="linenodiv"&gt;
       &lt;pre&gt;&lt;span class="normal"&gt;1&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/td&gt;
     &lt;td class="code"&gt;
      &lt;div&gt;
       &lt;pre&gt;&lt;span&gt;&lt;/span&gt;sh&lt;span class="w"&gt; &lt;/span&gt;./nix_install.sh&lt;span class="w"&gt; &lt;/span&gt;--no-daemon
&lt;/pre&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/div&gt;

&lt;/div&gt;

&lt;pre&gt;&lt;code&gt;performing a single-user installation of Nix...
copying Nix to /nix/store...
installing 'nix-2.20.1'
building '/nix/store/1ahlg3bviy174d6ig1gn393c23sqlki6-user-environment.drv'...
unpacking channels...
modifying /home/xy/.bash_profile...
modifying /home/xy/.zshenv...
placing /home/xy/.config/fish/conf.d/nix.fish...

Installation finished!  To ensure that the necessary environment
variables are set, either log in again, or type

. /home/xy/.nix-profile/etc/profile.d/nix.fish

in your shell.&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Wait!&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;modifying /home/xy/.bash_profile...
modifying /home/xy/.zshenv...
placing /home/xy/.config/fish/conf.d/nix.fish...&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;That&amp;rsquo;s very rude!&lt;/p&gt;

&lt;h3 id="stopping-nix-from-making-a-mess"&gt;Stopping Nix from making a mess&lt;/h3&gt;

&lt;p&gt;I need to prevent Nix from mess up with my environment when I do not want it to. Nix puts some code into the Bash, ZSH and Fish initialization files during installation to ease it&amp;rsquo;s use. I do not want that since I do not want Nix to meddle with my environment without me knowing it.&lt;/p&gt;

&lt;p&gt;I keep my &lt;code&gt;.bash_profile&lt;/code&gt; and &lt;code&gt;.zshenv&lt;/code&gt; in a stow-managed git repo so I can just &lt;code&gt;cd&lt;/code&gt; into my repo and do &lt;code&gt;git reset --hard&lt;/code&gt;, but for you will have to revert those files to their old forms manually.&lt;/p&gt;

&lt;h2 id="playing-with-nix"&gt;Playing with Nix&lt;/h2&gt;

&lt;p&gt;We do not have &lt;code&gt;nix&lt;/code&gt; in &lt;code&gt;PATH&lt;/code&gt; but we still can launch it. Nix executables are located inside &lt;code&gt;~/.nix-profile/bin/&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;By invoking &lt;code&gt;nix-shell&lt;/code&gt; one can create a ephemeral environment containing only packages specified after the &lt;code&gt;-p&lt;/code&gt; flag. I always add &lt;code&gt;-p nix&lt;/code&gt; to have the Nix tools available also inside the spawned environment.&lt;/p&gt;

&lt;p&gt;I will test out &lt;code&gt;chibi&lt;/code&gt; (small Scheme interpreter) + &lt;code&gt;rlwrap&lt;/code&gt; (REPL support for software lacking it) inside a Nix ephemeral environment:&lt;/p&gt;

&lt;div class="brush: shell"&gt;
 &lt;div class="source"&gt;
  &lt;table class="sourcetable"&gt;
   &lt;tbody&gt;
    &lt;tr&gt;
     &lt;td class="linenos"&gt;
      &lt;div class="linenodiv"&gt;
       &lt;pre&gt;&lt;span class="normal"&gt;1&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/td&gt;
     &lt;td class="code"&gt;
      &lt;div&gt;
       &lt;pre&gt;&lt;span&gt;&lt;/span&gt;~/.nix-profile/bin/nix-shell&lt;span class="w"&gt; &lt;/span&gt;-p&lt;span class="w"&gt; &lt;/span&gt;nix&lt;span class="w"&gt; &lt;/span&gt;chibi&lt;span class="w"&gt; &lt;/span&gt;rlwrap
&lt;/pre&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/div&gt;

&lt;/div&gt;

&lt;p&gt;Inside the spawned shell:&lt;/p&gt;

&lt;div class="brush: shell"&gt;
 &lt;div class="source"&gt;
  &lt;table class="sourcetable"&gt;
   &lt;tbody&gt;
    &lt;tr&gt;
     &lt;td class="linenos"&gt;
      &lt;div class="linenodiv"&gt;
       &lt;pre&gt;&lt;span class="normal"&gt;1&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/td&gt;
     &lt;td class="code"&gt;
      &lt;div&gt;
       &lt;pre&gt;&lt;span&gt;&lt;/span&gt;rlwrap&lt;span class="w"&gt; &lt;/span&gt;chibi-scheme
&lt;/pre&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/div&gt;

&lt;/div&gt;

&lt;p&gt;In the chibi REPL, let&amp;rsquo;s see the contents of the &lt;code&gt;PATH&lt;/code&gt; environment variable:&lt;/p&gt;

&lt;div class="brush: scheme"&gt;
 &lt;div class="source"&gt;
  &lt;table class="sourcetable"&gt;
   &lt;tbody&gt;
    &lt;tr&gt;
     &lt;td class="linenos"&gt;
      &lt;div class="linenodiv"&gt;
       &lt;pre&gt;&lt;span class="normal"&gt;1&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/td&gt;
     &lt;td class="code"&gt;
      &lt;div&gt;
       &lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;get-environment-variable&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s"&gt;"PATH"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/div&gt;

&lt;/div&gt;

&lt;p&gt;And exit the Scheme REPL:&lt;/p&gt;

&lt;div class="brush: shell"&gt;
 &lt;div class="source"&gt;
  &lt;table class="sourcetable"&gt;
   &lt;tbody&gt;
    &lt;tr&gt;
     &lt;td class="linenos"&gt;
      &lt;div class="linenodiv"&gt;
       &lt;pre&gt;&lt;span class="normal"&gt;1&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/td&gt;
     &lt;td class="code"&gt;
      &lt;div&gt;
       &lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;exit&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/div&gt;

&lt;/div&gt;

&lt;p&gt;After the playtime, run garbage collection:&lt;/p&gt;

&lt;div class="brush: shell"&gt;
 &lt;div class="source"&gt;
  &lt;table class="sourcetable"&gt;
   &lt;tbody&gt;
    &lt;tr&gt;
     &lt;td class="linenos"&gt;
      &lt;div class="linenodiv"&gt;
       &lt;pre&gt;&lt;span class="normal"&gt;1&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/td&gt;
     &lt;td class="code"&gt;
      &lt;div&gt;
       &lt;pre&gt;&lt;span&gt;&lt;/span&gt;~/.nix-profile/bin/nix-collect-garbage
&lt;/pre&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/div&gt;

&lt;/div&gt;</description></item></channel></rss>