<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:content="http://purl.org/rss/1.0/modules/content/">
  <channel>
    <title>Direnv on Haseeb Majid</title>
    <link>https://haseebmajid.dev/tags/direnv/</link>
    <description>Recent content in Direnv on Haseeb Majid</description>
    <generator>Hugo -- gohugo.io</generator>
    <language>en</language>
    <lastBuildDate>Thu, 12 Sep 2024 00:00:00 +0000</lastBuildDate><atom:link href="https://haseebmajid.dev/tags/direnv/index.xml" rel="self" type="application/rss+xml" />
    <item>
      <title>TIL: How to Get Shell Completions in Nix Shell With Direnv</title>
      <link>https://haseebmajid.dev/posts/2024-09-12-til-how-to-get-shell-completions-in-nix-shell-with-direnv/</link>
      <pubDate>Thu, 12 Sep 2024 00:00:00 +0000</pubDate>
      
      <guid>https://haseebmajid.dev/posts/2024-09-12-til-how-to-get-shell-completions-in-nix-shell-with-direnv/</guid>
      <description>&lt;p&gt;&lt;strong&gt;TIL: How to Get Shell Completions in Nix Shell With Direnv&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;When shell completions don&amp;rsquo;t work with direnv, you may need to use &lt;code&gt;nix develop&lt;/code&gt; to load the shell manually.&lt;/p&gt;
&lt;h2 id=&#34;background&#34;&gt;Background&lt;/h2&gt;
&lt;p&gt;I am using &lt;code&gt;nix-direnv&lt;/code&gt;, with nix devshell to autoload into my development environments. I changed the directory
and the devshell is automagically loaded without me doing anything, which is great. Provides me with a bunch of tools
specific for that project. With all the benefits of Nix, mainly reproducibility.&lt;/p&gt;</description>
      <content:encoded><![CDATA[<p><strong>TIL: How to Get Shell Completions in Nix Shell With Direnv</strong></p>
<p>When shell completions don&rsquo;t work with direnv, you may need to use <code>nix develop</code> to load the shell manually.</p>
<h2 id="background">Background</h2>
<p>I am using <code>nix-direnv</code>, with nix devshell to autoload into my development environments. I changed the directory
and the devshell is automagically loaded without me doing anything, which is great. Provides me with a bunch of tools
specific for that project. With all the benefits of Nix, mainly reproducibility.</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-bash" data-lang="bash"><span class="line"><span class="cl">❯ z banterbus/
</span></span><span class="line"><span class="cl">direnv: loading ~/projects/banterbus/.envrc
</span></span><span class="line"><span class="cl">direnv: using flake
</span></span><span class="line"><span class="cl">direnv: nix-direnv: Using cached dev shell
</span></span><span class="line"><span class="cl">direnv: <span class="nb">export</span> +AR +AS +CC +CONFIG_SHELL +CXX +GOTOOLDIR +HOST_PATH +IN_NIX_SHELL +LD +NIX_BINTOOLS +NIX_BINTOOLS_WRAPPER_TARGET_HOST_x86_64_unknown_linux_gnu +NIX_BUILD_CORES +NIX_CC +NIX_CC_WRAPPER_TARGET_HOST_x86_64_unknown_linux_gnu +NIX_CFLAGS_COMPILE +NIX_ENFORCE_NO_NATIVE +NIX_HARDENING_ENABLE +NIX_LDFLAGS +NIX_STORE +NM +OBJCOPY +OBJDUMP +PLAYWRIGHT_BROWSERS_PATH +PLAYWRIGHT_SKIP_BROWSER_DOWNLOAD +RANLIB +READELF +SIZE +SOURCE_DATE_EPOCH +STRINGS +STRIP +__structuredAttrs +buildInputs +buildPhase +builder +cmakeFlags +configureFlags +depsBuildBuild +depsBuildBuildPropagated +depsBuildTarget +depsBuildTargetPropagated +depsHostHost +depsHostHostPropagated +depsTargetTarget +depsTargetTargetPropagated +doCheck +doInstallCheck +dontAddDisableDepTrack +hardeningDisable +mesonFlags +name +nativeBuildInputs +out +outputs +patches +phases +preferLocalBuild +propagatedBuildInputs +propagatedNativeBuildInputs +shell +shellHook +stdenv +strictDeps +system ~PATH ~XDG_DATA_DIRS
</span></span></code></pre></div><p>Where my mkShell function looks something like:</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-nix" data-lang="nix"><span class="line"><span class="cl"><span class="p">{</span>
</span></span><span class="line"><span class="cl">  <span class="n">pkgs</span><span class="o">.</span><span class="n">mkShell</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl">    <span class="n">hardeningDisable</span> <span class="o">=</span> <span class="p">[</span><span class="s2">&#34;all&#34;</span><span class="p">];</span>
</span></span><span class="line"><span class="cl">    <span class="n">shellHook</span> <span class="o">=</span> <span class="s1">&#39;&#39;
</span></span></span><span class="line"><span class="cl"><span class="s1">      export PLAYWRIGHT_SKIP_BROWSER_DOWNLOAD=1
</span></span></span><span class="line"><span class="cl"><span class="s1">      export PLAYWRIGHT_BROWSERS_PATH=&#34;</span><span class="si">${</span><span class="n">pkgs</span><span class="o">.</span><span class="n">playwright-driver</span><span class="o">.</span><span class="n">browsers</span><span class="si">}</span><span class="s1">&#34;
</span></span></span><span class="line"><span class="cl"><span class="s1">      </span><span class="si">${</span><span class="n">pre-commit-check</span><span class="o">.</span><span class="n">shellHook</span><span class="si">}</span><span class="s1">
</span></span></span><span class="line"><span class="cl"><span class="s1">    &#39;&#39;</span><span class="p">;</span>
</span></span><span class="line"><span class="cl">    <span class="n">buildInputs</span> <span class="o">=</span> <span class="n">pre-commit-check</span><span class="o">.</span><span class="n">enabledPackages</span><span class="p">;</span>
</span></span><span class="line"><span class="cl">    <span class="n">packages</span> <span class="o">=</span> <span class="k">with</span> <span class="n">pkgs</span><span class="p">;</span> <span class="p">[</span>
</span></span><span class="line"><span class="cl">      <span class="c1"># TODO: workout how to use go env</span>
</span></span><span class="line"><span class="cl">      <span class="c1"># goEnv</span>
</span></span><span class="line"><span class="cl">      <span class="n">gomod2nix</span>
</span></span><span class="line"><span class="cl">      <span class="n">go_1_22</span>
</span></span><span class="line"><span class="cl">      <span class="n">playwright-test</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl">      <span class="n">goose</span>
</span></span><span class="line"><span class="cl">      <span class="n">air</span>
</span></span><span class="line"><span class="cl">      <span class="n">golangci-lint</span>
</span></span><span class="line"><span class="cl">      <span class="n">gotools</span>
</span></span><span class="line"><span class="cl">      <span class="n">gotestsum</span>
</span></span><span class="line"><span class="cl">      <span class="n">gocover-cobertura</span>
</span></span><span class="line"><span class="cl">      <span class="n">go-task</span>
</span></span><span class="line"><span class="cl">      <span class="n">go-mockery</span>
</span></span><span class="line"><span class="cl">      <span class="n">goreleaser</span>
</span></span><span class="line"><span class="cl">      <span class="n">golines</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl">      <span class="n">tailwindcss</span>
</span></span><span class="line"><span class="cl">      <span class="n">templ</span>
</span></span><span class="line"><span class="cl">      <span class="n">sqlc</span>
</span></span><span class="line"><span class="cl">    <span class="p">];</span>
</span></span><span class="line"><span class="cl">  <span class="p">}</span>
</span></span><span class="line"><span class="cl"><span class="p">}</span>
</span></span></code></pre></div><p>You can see I have certain tools I want other developers to have access to like <code>go-task</code>, which they will automatically
get when they load into the devshell.</p>
<h2 id="issue">Issue</h2>
<p>This is all great; however, I have noticed that shell completions stopped working. Where I can normally do <code>task &lt;TAB&gt;</code></p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-bash" data-lang="bash"><span class="line"><span class="cl">❯ task tests:e2e
</span></span><span class="line"><span class="cl">build:dev  <span class="o">(</span>Build the app <span class="k">for</span> development, generates all the files needed <span class="k">for</span> the binary.<span class="o">)</span>  generate:sqlc  <span class="o">(</span>Generates the code to interact with SQL DB.<span class="o">)</span>
</span></span><span class="line"><span class="cl">coverage                            <span class="o">(</span>Run the integration tests and gets the code coverage<span class="o">)</span>  lint                                      <span class="o">(</span>Runs the linter.<span class="o">)</span>
</span></span><span class="line"><span class="cl">dev                                       <span class="o">(</span>Start the app in dev mode with live-reloading.<span class="o">)</span>  release                              <span class="o">(</span>Release the CLI tool.<span class="o">)</span>
</span></span><span class="line"><span class="cl">docker:build                                            <span class="o">(</span>Builds a Docker image using Nix.<span class="o">)</span>  tests                                  <span class="o">(</span>Runs all the tests.<span class="o">)</span>
</span></span><span class="line"><span class="cl">docker:load                                <span class="o">(</span>Loads the Docker image from tar <span class="o">(</span>in results<span class="o">)</span>.<span class="o">)</span>  tests:e2e                  <span class="o">(</span>Runs e2e tests with playwright.<span class="o">)</span>
</span></span><span class="line"><span class="cl">docker:publish                                                <span class="o">(</span>Publishes the Docker image<span class="o">)</span>  tests:integration          <span class="o">(</span>Runs all the integration tests.<span class="o">)</span>
</span></span><span class="line"><span class="cl">format                                                               <span class="o">(</span>Runs the formatter.<span class="o">)</span>  tests:unit                        <span class="o">(</span>Runs all the unit tests.<span class="o">)</span>
</span></span></code></pre></div><p>However, in the devshell this does not work until I load into the shell manually <code>nix develop</code>. Which does defeat
the purpose of using <code>nix-direnv</code> as you need to manually run a command now.</p>
<p>I don&rsquo;t know what causes this and will do a deeper dive at some point 🤔.</p>
]]></content:encoded>
    </item>
    
    <item>
      <title>TIL: How to Set up Layouts in Zellij That Use Direnv and Nix</title>
      <link>https://haseebmajid.dev/posts/2024-03-18-til-how-to-setup-layouts-in-zellij-that-use-direnv-and-nix/</link>
      <pubDate>Mon, 18 Mar 2024 00:00:00 +0000</pubDate>
      
      <guid>https://haseebmajid.dev/posts/2024-03-18-til-how-to-setup-layouts-in-zellij-that-use-direnv-and-nix/</guid>
      <description>&lt;p&gt;&lt;strong&gt;TIL: How to Set up Layouts in Zellij That Use Direnv and Nix&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;I have been using &lt;a href=&#34;https://github.com/zellij-org/zellij&#34;&gt;Zellij&lt;/a&gt; for a while now. I tried to set up layouts for
one of my personal projects. So that we could have tests and linting running and any other tasks we may want
whilst doing development &lt;sup id=&#34;fnref:1&#34;&gt;&lt;a href=&#34;#fn:1&#34; class=&#34;footnote-ref&#34; role=&#34;doc-noteref&#34;&gt;1&lt;/a&gt;&lt;/sup&gt;.&lt;/p&gt;
&lt;p&gt;However, I had an issue working out how to call commands that required direnv and nix to set up development environments.
In my case, my nix dev shell installed the go-task tool to run tasks such as &lt;code&gt;task lint&lt;/code&gt; or &lt;code&gt;task tests&lt;/code&gt;. However
the shell that zellij was running the commands in for the layout did not load my Nix dev shell, so the go-task binary
was not available.&lt;/p&gt;</description>
      <content:encoded><![CDATA[<p><strong>TIL: How to Set up Layouts in Zellij That Use Direnv and Nix</strong></p>
<p>I have been using <a href="https://github.com/zellij-org/zellij">Zellij</a> for a while now. I tried to set up layouts for
one of my personal projects. So that we could have tests and linting running and any other tasks we may want
whilst doing development <sup id="fnref:1"><a href="#fn:1" class="footnote-ref" role="doc-noteref">1</a></sup>.</p>
<p>However, I had an issue working out how to call commands that required direnv and nix to set up development environments.
In my case, my nix dev shell installed the go-task tool to run tasks such as <code>task lint</code> or <code>task tests</code>. However
the shell that zellij was running the commands in for the layout did not load my Nix dev shell, so the go-task binary
was not available.</p>
<p>So to fix this, we need to do something like this <code>layout.kdl</code>:</p>
<pre tabindex="0"><code class="language-kdl" data-lang="kdl">layout {
    pane size=1 borderless=true {
        plugin location=&#34;zellij:tab-bar&#34;
    }
    pane {
        command &#34;fish&#34;
        args &#34;-c&#34; &#34;direnv exec . task lint&#34;
    }
    pane {
        command &#34;fish&#34;
        args &#34;-c&#34; &#34;direnv exec . task tests&#34;
    }
    pane {
        command &#34;docker-compose&#34;
        args &#34;up&#34; &#34;--build -d&#34;
    }
}
</code></pre><p>Where the key being we need to execute the direnv environment i.e. <code>direnv exec .</code>, then we can run commands like
normal. Where direnv <code>.envrc</code> file contains <code>use flake</code>. That&rsquo;s it! If you want Zellij to use direnv and load your
nix flake dev shell, you have to add a bit of boilerplate, which is not ideal but</p>
<div class="footnotes" role="doc-endnotes">
<hr>
<ol>
<li id="fn:1">
<p><a href="https://github.com/zellij-org/zellij/issues/2294">https://github.com/zellij-org/zellij/issues/2294</a>&#160;<a href="#fnref:1" class="footnote-backref" role="doc-backlink">&#x21a9;&#xfe0e;</a></p>
</li>
</ol>
</div>
]]></content:encoded>
    </item>
    
  </channel>
</rss>
