Fixing error: attribute 'currentSystem' missing in flake

Hi there, I’m a beginner to Nix (think it’s super cool so far) - just having an issue with creating a flake to develop Golang with. I’ve been trying to use an old version of the golang lint package: Nix Package Versions
My flake.nix is:

{
  description = "golang development environment ";
inputs = {
    flake-utils.url = "github:numtide/flake-utils";
        nixpkgs.url = "github:nixos/nixpkgs/nixos-unstable";

  };

  outputs = { self, nixpkgs, flake-utils }:
    flake-utils.lib.eachDefaultSystem
      (system:
        let pkgs = nixpkgs.legacyPackages.${system}; in
        {
          system = builtins.currentSystem;
          devShell = import ./shell.nix { inherit pkgs; };
        }
      );
}

And the shell is:

{ pkgs ? import <nixpkgs> {} }:

with pkgs;
let
    pkgs = import (builtins.fetchGit {
         # Descriptive name to make the store path easier to identify                
         name = "my-old-revision";                                                 
         url = "https://github.com/NixOS/nixpkgs/";                       
         ref = "refs/heads/nixpkgs-unstable";                     
         rev = "ff8b619cfecb98bb94ae49ca7ceca937923a75fa";                                           
     }) {};                                                                           

     myPkg = pkgs.golangci-lint;
in
mkShell {
  nativeBuildInputs = [
    go_1_18
    gopls
    tmux
    gofumpt
    myPkg
    gosec
    delve
    go-tools
    gotests
    gomodifytags
  ];
}

The error I get is:

error: attribute 'currentSystem' missing

       at /nix/store/lgbjq8aqhz9l0n607klg4b0l4nh19fba-my-old-revision/pkgs/top-level/impure.nix:17:43:

           16|   # (build, in GNU Autotools parlance) platform.
           17|   localSystem ? { system = args.system or builtins.currentSystem; }
             |                                           ^
           18|
(use '--show-trace' to show detailed location information)

I assume the error is something to do with impurities with bringing in the system, but I’m not sure how to declare it as input. Many thanks in advance for the help!

1 Like

You’re basically pinning nixpkgs twice, once explicitly in shell.nix when you assign to pkgs and once implicitly by using flakes.

I think you can use nix flake update with some flags to set nixpkgs to the specific revision, or if you only want the pinned nixpkgs for that package, you can additionally pass system to your shell.nix and pass it through to where you import nixpkgs from the pinned version.

(I’m on my phone so I can’t really post code, hope that helps otherwise I’ll elaborate tomorrow)

Yes, builtins.currentSystem does not intentionally work in flakes. You need to pass system attribute explicitly as an attribute to the imported Nixpkgs. You can add system as a shell.nix attribute and pass it in flake.nix.

The builtins.currentSystem in your flake.nix does not cause an issue because Nix is lazy and outputs.system.${…} attribute will not be evaluated when building the shell.

1 Like

Hey thanks for your answer. How do I implement this?
I’ve tried adding the line: system = builtins.currentSystem;
To the let statement in shell.nix and inside the mkShell curly braces but neither seem to work

You will need to pass it down through the imports from the value of the system argument of the function passed to flake-utils.lib.eachDefaultSystem.

Hey thanks again…I can’t seem to make it work. I’ve refactored it to all be in flake like this:

{
  description = "golang development environment ";
  
inputs = {
    flake-utils.url = "github:numtide/flake-utils";
    nixpkgs.url = "github:nixos/nixpkgs/nixos-unstable";
    

  };

  outputs = { self, nixpkgs, flake-utils }:
    flake-utils.lib.eachDefaultSystem
      (system:
        let pkgs = nixpkgs.legacyPackages.${system}; 
        pkgs.currentSystem = system;
        pkgs = import (builtins.fetchGit {
         # Descriptive name to make the store path easier to identify                
         name = "my-old-revision";                                                 
         url = "https://github.com/NixOS/nixpkgs/";                       
         ref = "refs/heads/nixpkgs-unstable";                     
         rev = "ff8b619cfecb98bb94ae49ca7ceca937923a75fa";                                           
          }) {};    
          myPkg = pkgs.golangci-lint;
        in
        {
          # devShell = import ./shell.nix { inherit pkgs; };
          devShell = pkgs.mkShell { buildInputs = [ 
              pkgs.go_1_18
              pkgs.gopls
              pkgs.tmux
              pkgs.gofumpt
              myPkg
              pkgs.gosec
              pkgs.delve
              pkgs.go-tools
              pkgs.gotests
              pkgs.gomodifytags
              ]; currentSystem = system; };
        }
      );
}

But I get the error that pkgs.currentSystem = system; is not defined. Sorry for my absolute noobness :slight_smile:

You cannot really update existing variables like this in Nix.

I meant something like the following:

-{ pkgs ? import <nixpkgs> {} }:
+{ system ? builtins.currentSystem, pkgs ? import <nixpkgs> { inherit system; } }:
 
 with pkgs;
 let
     pkgs = import (builtins.fetchGit {
          # Descriptive name to make the store path easier to identify                
          name = "my-old-revision";                                                 
          url = "https://github.com/NixOS/nixpkgs/";                       
          ref = "refs/heads/nixpkgs-unstable";                     
          rev = "ff8b619cfecb98bb94ae49ca7ceca937923a75fa";                                           
-     }) {};
+    }) {
+      inherit system;
+    };                                                       
   outputs = { self, nixpkgs, flake-utils }:
     flake-utils.lib.eachDefaultSystem
       (system:
         let pkgs = nixpkgs.legacyPackages.${system}; in
         {
-          system = builtins.currentSystem;
-          devShell = import ./shell.nix { inherit pkgs; };
+          devShell = import ./shell.nix { inherit pkgs system; };
         }
       );
3 Likes

What a legend! Thank you very much that worked