Defined function errors as undefined?

I’ve copied a function declaration from this issue comment, but I can’t get it to run without errors.

It returns this when doing a nixos-rebuild switch:

error: undefined variable 'mkJob' at /etc/nixos/configuration.nix:84:22
(use '--show-trace' to show detailed location information)
building Nix...
error: undefined variable 'mkJob' at /etc/nixos/configuration.nix:84:22
(use '--show-trace' to show detailed location information)
building the system configuration...
error: undefined variable 'mkJob' at /etc/nixos/configuration.nix:84:22
(use '--show-trace' to show detailed location information)

The relevant code I have in configuration.nix is the following, where “user” is a valid user:

  mkJob = user: {
    "syncthing-${user}" = {
      description = "Syncthing service for user ${user}";
      after = [ "network.target" ];
      environment = { STNOUPGRADE = "yes"; };
      wantedBy = [ "multi-user.target" ];
      serviceConfig = {
        Restart = "on-failure";
        SuccessExitStatus = "2 3 4";
        RestartForceExitStatus="3 4";
        User = user;
        Group = "users";
        PermissionsStartOnly = true;
        ExecStart = "${config.services.syncthing.package}/bin/syncthing -no-restart -no-browser -home=/var/lib/syncthing/${user}";
      };
    };
  };

  systemd.services = mkJob "user";

I don’t understand how it’s undefined. Is it failing to recognize it as a function?

I can’t tell from just this snippet but it looks to me like you may have defined mkJob as an attribute in the configuration attrset, instead of as a let binding above the attrset.

That’s done it, thanks. I’m having trouble learning the language. I thought I had seen functions defined and used in that way before, I guess not. There wouldn’t happen to be a formal grammar specification anywhere, would there?

A function could be defined and used that way in a rec attrset, but that also leaves the function as part of the configuration attrset itself which is a bit odd.

1 Like