How to build the Zig language server using zig-overlay nix flake?

Hi ! I would like to build zls with the zig version of zig-overlay in my nix shell. i tried to override the nativeBuildInputs of the zls flake like this

zig = zig_overlay.packages.${system}.master-2024-01-07;
zlsOverride = inputs.zls // { nativeBuildInputs = [ zig ]; };
zls = zlsOverride.packages.${system}.zls;

it compiles but zls always builds with the latest version and not the one I specified.
is there any way i can do what i want to do? i’m pretty new to nix…

I haven’t tried it but this should work:

zls = pkgs.zls.overrideAttrs(prev: {
  nativeBuildInputs = [ zig ];
});

Otherwise you can just replace Nixpkgs’ Zig with your own in an overlay:

pkgs = import nixpkgs {
  inherit system;
  overlays = [
    (final: prev: {
      inherit zig; # from zig = zig_overlay.packages.${system}.master-2024-01-07
    })
  ];
};

This will build every package you get from Nixpkgs with that specific version of Zig.

2 Likes

Good evening folks. Apologies for the continued question. I have the same issue as Wydra in that I’d like to have my version of ZIG and ZLS have the same version in a devShell.

My (very basic) understanding of what should happen is:

1: Use the overlay from the mitchellh/zig_overly flake and specify a more current version of zig
2: Create an overlay to override nixpkgs with this more recent version of zig.
3: Then override the zig compiler use to build zls with the more recent version of zig.
4: Add zls and zig to the packages in the devShell

This leads to me creating a flake like this:

{
  description = "zls zig take 400";

  inputs = {
  
    nixpgks.url = "github:nixos/nixpkgs/nixos-unstable";
    zig_overlay.url = "github:mitchellh/zig-overlay";
    zig_overlay.inputs.nixpkgs.follows = "nixpkgs";
  }; #end input

  outputs = {
    self,
    nixpkgs,
    zig_overlay,
    ...
  } @ inputs:
  let
   system ="x86_64-linux"; 
   zig = zig_overlay.packages.${system}.master;
   overlays = [
     (final: prev: {
      inherit zig; # from zig = zig_overlay.packages.${system}.master
    })
   ];
   pkgs = import nixpkgs {inherit system overlays;};
   zls = pkgs.zls.overrideAttrs(prev:{nativeBuildInputs = [zig];});
  in
  {
    devShells.${system}.default = 
    pkgs.mkShell {
      packages = [
          zig
          zls
      ];
    };
  };

}

However if I run : nix develop -c $SHELL I get the following error:

error: builder for '/nix/store/0hcc5kc8pza9m18cvfysqq6ra51hxai2-zls-0.12.0.drv' failed with exit code 1;
       last 6 log lines:
       > Running phase: unpackPhase
       > unpacking source archive /nix/store/m44q9mf6fclpmnraimgnqxj9284q8fr9-source
       > source root is source
       > Running phase: patchPhase
       > ln: failed to create symbolic link '/p': Permission denied
       > /nix/store/xfhkjnpqjwlf6hlk1ysmq3aaq80f3bjj-stdenv-linux/setup: line 131: pop_var_context: head of shell_variables not a function context
       For full logs, run 'nix log /nix/store/0hcc5kc8pza9m18cvfysqq6ra51hxai2-zls-0.12.0.drv'.
error: 1 dependencies of derivation '/nix/store/s7dw83qsc8hzk63qfi0brx451dz6pwp1-nix-shell-env.drv' failed to build

I thought I had a grasp of this, but alas! Any help or input would be greatly appreciated!

1 Like

I’m also trying to do the exact thing and having trouble

I finally got this flake to work: The Zig overlay between default and master can be changed to if you would want to use the current tagged release (0.13 currently) or the “nighlty”

Hope this helps you!

{
  inputs = rec {
    nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
    zig-overlay.url = "github:mitchellh/zig-overlay";
    zls-overlay.url = "github:zigtools/zls";
  };
  outputs = inputs @ {
    self,
    nixpkgs,
    ...
  }:
  let
    pkgs = nixpkgs.legacyPackages.x86_64-linux;
    zig = inputs.zig-overlay.packages.x86_64-linux.default;
    # zig = inputs.zig-overlay.packages.x86_64-linux.master;
    zls = inputs.zls-overlay.packages.x86_64-linux.zls.overrideAttrs (old: {
            nativeBuildInputs = [ zig ];
          });
  in
  {
    devShells.x86_64-linux.default = pkgs.mkShell {
      packages = with pkgs; [
        zls
        zig
     
      ];
    };
  };
}