How to set top-level global variable in flake.nix?

I want to replace all occurrences of string 25.05 with references to top-level global variable systemVersion in the flake.nix.

sudo nixos-rebuild switch --flake .#<hostname> produces the following error:

error:
       … while evaluating the file '/nix/store/8pn89la8gksmhwxr3p66j50q2smwbwnl-source/flake.nix':

       error: file '/nix/store/8pn89la8gksmhwxr3p66j50q2smwbwnl-source/flake.nix' must be an attribute set

Code:

let
  systemVersion = "25.05";
in {
  description = "whiteman808's config for NixOS";

  inputs = {
    ## NixOS ecosystem
    nixpkgs.url = "github:NixOS/nixpkgs/nixos-${systemVersion}";
    nixpkgs-unstable.url = "nixpkgs/nixos-unstable";

    # home-manager, used for managing user configuration
    home-manager = {
      url = "github:nix-community/home-manager/release-${systemVersion}";
      # The `follows` keyword in inputs is used for inheritance.
      # Here, `inputs.nixpkgs` of home-manager is kept consistent with
      # the `inputs.nixpkgs` of the current flake,
      # to avoid problems caused by different versions of nixpkgs.
      inputs.nixpkgs.follows = "nixpkgs";
    };

    # credential management in flake
    sops-nix = {
      url = "github:Mic92/sops-nix";
      inputs.nixpkgs.follows = "nixpkgs";
    };
  };
  
  <other-stuff-here>
}

You can’t and shouldn’t do that. Just maintain it per config.

extra context: What language is flake.nix written in? · Issue #4945 · NixOS/nix · GitHub

try this :wink: (also mentioned in the issue comment linked above)

That really isn’t a good idea. The point of not having eval in the meta context is to prevent complex evaluation when running simple commands on flakes, especially with long dependency chains. This is a deliberate design decision to prevent ecosystem issues, not a bug to be fixed.

This will never land upstream, so any flakes you write will forever be unusable by anyone but you if you use that patch.

1 Like

This is a deliberate design decision to prevent ecosystem issues, not a bug to be fixed.

I know but it is annoying not to be able to write whatever I like in flake.nix

so any flakes you write will forever be unusable by anyone but you if you use that patch

That’s fine for me :rofl: