How do I evaluate a string concatenation without it becoming a thunk?

I’m trying to create a nix flake for home manager, and wanting to do it where I have a variable with the version of nixpkgs. However, I get the error:

error: expected a string but got a thunk at /nix/store/c5nmpb64cv23mglknw1n7a2k3cjgz3ys-source/nix-home-manager-gui/.config/nixpkgs/flake.nix:7:3

       … in flake attribute 'url'

       at /nix/store/c5nmpb64cv23mglknw1n7a2k3cjgz3ys-source/nix-home-manager-gui/.config/nixpkgs/flake.nix:7:3:

            6|          # Get both NixOS state and NixOS unstable
            7|          nixpkgs.url = "github:NixOS/nixpkgs/nixos-" + base-state-version;
             |   ^
            8|          nixpkgs-unstable.url = "github:NixOS/nixpkgs/nixos-unstable";

This is how I’ve got things at the moment in my flake.nix:

rec {
	# Define the common state version
	base-state-version = "22.11";

	inputs = {
		# Get both NixOS state and NixOS unstable
		nixpkgs.url = "github:NixOS/nixpkgs/nixos-" + base-state-version;
		nixpkgs-unstable.url = "github:NixOS/nixpkgs/nixos-unstable";

		# Get home-manager
		home-manager.url = "github:nix-community/home-manager/release-" + base-state-version;
		home-manager.inputs.nixpkgs.follows = "nixpkgs";
	};
    .....

Searching around, I cannot find how to non-lazily make a string such that it doesn’t become a thunk. Does anyone have any thoughts?

Also, is there a way to do this without the rec? Or is this necessary? I’m not too sure on what the effects of using this command.

I don’t think that’s possible. The flake format have some limitations on what can be and cannot be used in flake inputs

Instead of having a separate base-state-version, I would recommend hardcoding it like nixpkgs.url = "github:NixOS/nixpkgs/nixos-22.11, and the value can be accessed in the outputs with nixpkgs.lib.version, and you would have to update the url for home-manager manually as well

1 Like

Hmm… ok. I was hoping to save a bit of writing code but if not then I’ll do without.