blog/content/posts/2022/01/zig-direnv.md
2022-06-25 08:47:06 +01:00

1.6 KiB

title date tags
Quick reproducible zig dev env using nix flakes 2022-01-01
zig
nix

Want this great feeling of entering a directory and having all your dev setup done but can never remember how to set up nix flakes ?

What you'll need is, in addition to a working direnv with nix and nix flake enabled, is:

.envrc:

use flake

.gitignore:

.direnv/

flake.nix (this nixpkgs hash points to the first revision with zig 0.9 present):

{
  inputs = {
    nixpkgs = {
      type = "github";
      owner = "nixos";
      repo = "nixpkgs";
      ref = "e1eeb53e64159fbb2610ba7810ed511e4d5c10ca";
    };
    flake-compat = {
      url = "github:edolstra/flake-compat";
      flake = false;
    };
  };

  outputs = { self, nixpkgs, flake-compat }:
    let pkgs = nixpkgs.legacyPackages.x86_64-linux;
    in
    {
      devShell.x86_64-linux =
        pkgs.mkShell {
          buildInputs = with pkgs;[
            nixpkgs-fmt
            zig
            zls
          ];
        };
    };
}

and some shell.nix for this extra bit of nix-shell compatibility, if you'd want it:

(import
  (
    let
      lock = builtins.fromJSON (builtins.readFile ./flake.lock);
    in
    fetchTarball {
      url = "https://github.com/edolstra/flake-compat/archive/${lock.nodes.flake-compat.locked.rev}.tar.gz";
      sha256 = lock.nodes.flake-compat.locked.narHash;
    }
  )
  {
    src = ./.;
  }).shellNix

Don't forget to:

  • git add all of the above, otherwise nix flake operations might not work
  • direnv allow this directory
  • have fun !