Flake nix run .#package exec format error affer 24.11

I just upgraded from 24.05 to 24.01.

Until now I’d been using this helper script instead of the full nixos-rebuild switch command:

    let
      pkgs = nixpkgs.legacyPackages.x86_64-linux;
    in
    {
      nixosConfigurations.deck16 = nixpkgs.lib.nixosSystem {
        system = "x86_64-linux";
        modules = [
          # ...
        ];
      };

      packages.x86_64-linux.switch = pkgs.writeScriptBin "switch" ''
        sudo ${pkgs.nixos-rebuild}/bin/nixos-rebuild switch --flake path:.#deck16
      '';


then switching config via nix run .#switch. This worked well until I switched to 24.11. Now, when I run the command I get Exec format error

I don’t understand how this can be because I’m on x86_64 and the package I’m exporting is packages.x86_64-linux which matches the system it’s running on. The flake contains no references to any other architecture because the system is only installed on x86_64 machines.

Nothing changed in the flake but since the update. I can’t see anything in the backwards incompatible release notes about this.

edit: if I run the script directly from the nix store it works:

/nix/store/lpqdmbcyy6rf8zwmh6ls1mvixfhnbsqy-switch/bin/switch

unsurprisingly, since it’s just a bash script

so it’s something to do with using nix run to launch it that’s the issue

To answer my own question, to fix it I added #!${pkgs.bash}/bin/bash to the script. A little strange that it worked until 24.11 though.

You might want to consider using pkgs.writeShellScriptBin instead which prepends the shell’s shebang for you.