Recommended Nix template for zig development?

Anyone else use Nix flake-based development environments? I am curious to see how others approach this, maybe I can improve my own setup!

I use it to manage both my zig versions as well as my development environment. I essentially use Mitchell Hashimoto’s overlay and build ZLS from scratch with the specified zig version. Seems to work.

My best go as a Nix novice:

I’ve never used flakes before. I use nix-shell with these two lines:

    hardeningDisable = [ "all" ];
    buildInputs = [ cmake ];
1 Like

I personally use the below.

  • silversquirl/zig-flake is a bit faster with nightlies
  • (if you use zls) I personally like keeping the version of zls project specific
  • I don’t need another c compiler, so I keep it out of my shell
  • Outside of that I emulate forEachSystem to have one less dependency
{
  description = "Zig dev environment";

  inputs = {
    nixpkgs.url = "github:nixos/nixpkgs/nixos-unstable";

    zig = {
      url = "github:silversquirl/zig-flake/compat";
      inputs.nixpkgs.follows = "nixpkgs";
    };

    zls = {
      url = "github:zigtools/zls";
      inputs.nixpkgs.follows = "nixpkgs";
    };
  };

  outputs =
    {
      nixpkgs,
      zig,
      zls,
      ...
    }:
    let
      forAllSystems = f: builtins.mapAttrs f nixpkgs.legacyPackages;
    in
    {
      devShells = forAllSystems (
        system: pkgs: {

          default = pkgs.mkShellNoCC {
            packages = [
              zig.packages.${system}.nightly
              zls.packages.${system}.zls
            ];
          };
        }
      );
    };
}
2 Likes