Cannot assign the value of a variable to another

Hello! I am having trouble assigning the value of one variable to another. Basically, I am trying to configure MPD and the code in my configuration looks like this:

  users.users.nick = {
    name = "nick";
    uid = 1000;
    home = "/home/${config.users.users.nick.name}";
    isNormalUser = true;
    shell = pkgs.zsh;
    extraGroups = [
      "wheel"
      "networkmanager"
    ];
  };

......................................

  services.mpd = {
    enable = true;
    user = config.users.users.nick.name;
    musicDirectory = ${config.users.users.nick.home}/store/music";
};

The problem seems to be that I am trying to set services.mpd.user = config.users.users.nick.name, but I have no idea why this is. I am also having trouble reproducing the error message, as I now get

error: cached failure of attribute ‘nixosConfigurations.hermes.config.system.build.toplevel’

when running nixos-rebuild on my configuration flake.

I tried running this with Lix instead of Nix, and instead get an infinite recursion error:

error:
       … while calling the 'head' builtin
         at /nix/store/5z81la6av4j0ckp0w1949lxiwjyyykks-source/lib/attrsets.nix:1575:11:
         1574|         || pred here (elemAt values 1) (head values) then
         1575|           head values
             |           ^
         1576|         else

       … while evaluating the attribute 'value'
         at /nix/store/5z81la6av4j0ckp0w1949lxiwjyyykks-source/lib/modules.nix:809:9:
          808|     in warnDeprecation opt //
          809|       { value = builtins.addErrorContext "while evaluating the option `${showOption loc}':" value;
             |         ^
          810|         inherit (res.defsFinal') highestPrio;

       (stack trace truncated; use '--show-trace' to show the full trace)

       error: infinite recursion encountered
       at /nix/store/5z81la6av4j0ckp0w1949lxiwjyyykks-source/lib/modules.nix:809:9:
          808|     in warnDeprecation opt //
          809|       { value = builtins.addErrorContext "while evaluating the option `${showOption loc}':" value;
             |         ^
          810|         inherit (res.defsFinal') highestPrio;

Interestingly, evaluation passes if I remove services.mpd.user.

I guess you could define the username outside of the main code like so:

let
  username = "nick";
in
  users.users."${username}" = {
    name = username;
    uid = 1000;
    home = "/home/${config.users.users.nick.name}";
    isNormalUser = true;
    shell = pkgs.zsh;
    extraGroups = [
      "wheel"
      "networkmanager"
    ];
  };

  services.mpd = {
    enable = true;
    user = username;
    musicDirectory = "${config.users.users.${username}.home}/store/music";
};

I ended up doing something really similar to your solution and it fixed my problem. Thanks a lot. Still, it feels a little hacky and inconsistent… maybe it’s a Nix issue.

1 Like