Zig dev shell with Nix flakes and mitchellh/zig-overlay
Introduction
Note: If you’re already comfortable with flakes and overlays, this post may be boring to you. This is more for my own memory than anything else. I do recommend reading the posts I’ve linked out to if you are unfamiliar with anything below.
I’ve used Nix for a while – on my Mac with nix-darwin and home-manager, dev environments with devenv.sh, and even for this blog – but I’m no expert. Today I finally grokked overlays after reading this awesome blog post, and wrote a minimal Nix flake for Zig.
Mitchell Hashimoto maintains a Zig
overlay that I’d been using via
nix shell 'github:mitchellh/zig-overlay#master'
or in devenv. This time I
wanted to use it in a flake and share the result.
The Flake
I’m on a MacBook with Apple silicon, but I also use a Fedora laptop, so I
wanted the flake to work on both. I avoided flake-utils
on purpose – partly to
learn, partly to see what’s really going on without convenience helpers. Here’s
the flake with comments:
# flake.nix
{
description = "A very basic flake";
inputs = {
nixpkgs.url = "github:nixos/nixpkgs?ref=nixos-unstable";
zig = {
url = "github:mitchellh/zig-overlay";
inputs = {
nixpkgs.follows = "nixpkgs";
};
};
};
outputs =
{
self,
zig,
nixpkgs,
}:
let
systems = [
"aarch64-darwin"
"x86_64-linux"
];
# see https://nixos.org/manual/nixpkgs/stable/#function-library-lib.attrsets.genAttrs
# we're going to use this function below to define dev shells for all
# systems. the flake-utils package has an `eachSystem` function that
# accomplishes something similar.
forAllSystems = nixpkgs.lib.genAttrs systems;
in
{
devShells = forAllSystems (
system:
let
pkgs = import nixpkgs {
inherit system;
# overlay is going to make pkgs.zigpkgs available,
# which allows us to install whatever version of Zig we want
# including nightly (which I’m using below).
overlays = [ zig.overlays.default ];
};
in
{
default = pkgs.mkShell {
nativeBuildInputs = with pkgs; [
# Going to be hooking into libgit2 for this particular project.
libgit2
# install the nightly build
zigpkgs.master
];
};
}
);
};
}
Now you should be able to do something like the following:
~/workspace/zig/git-lore/ > nix develop --command $SHELL
~/workspace/zig/git-lore/ > zig version
0.16.0-dev.23+47a2f2dda
~/workspace/zig/git-lore/ >
Closing
That’s it! Nothing groundbreaking, but it was fun to get set up. I’ve got a few other Nix-y posts in mind that might be more interesting. Let me know if there’s anything you’d like me to dive into.
Flake without the comments or libgit2
dependency provided below for your
convenience.
{
description = "A very basic flake";
inputs = {
nixpkgs.url = "github:nixos/nixpkgs?ref=nixos-unstable";
zig = {
url = "github:mitchellh/zig-overlay";
inputs = {
nixpkgs.follows = "nixpkgs";
};
};
};
outputs =
{
self,
zig,
nixpkgs,
}:
let
systems = [
"aarch64-darwin"
"x86_64-linux"
];
forAllSystems = nixpkgs.lib.genAttrs systems;
in
{
devShells = forAllSystems (
system:
let
pkgs = import nixpkgs {
inherit system;
overlays = [ zig.overlays.default ];
};
in
{
default = pkgs.mkShell {
nativeBuildInputs = with pkgs; [
zigpkgs.master
];
};
}
);
};
}