NixOS Module Options are causing rebuild to fail

I am trying to parametrize my NixOS system configuration, so that I may use it across different machines (there are many similarities in my setup, I want the differences to be stored in “machine profile” file).

I have decided to wrap whole configuration under a single NixOS module.

So far I had:

  1. Created dedicated NixOS module
  2. Defined it’s options (they are intended as system configuration params)
  3. Added new module to configuration.nix as import
  4. Attempted to run nixos-rebuild switch

The rebuild fails with the following error:

Error message:

$ nixos-rebuild switch
building Nix...
building the system configuration...
error: value is a string while a set was expected, at /nix/var/nix/profiles/per-user/root/channels/nixos/lib/modules.nix:220:18
(use '--show-trace' to show detailed location information)

When using “nix repl”, the module options are set to correct values (i.e. username is a string with expected value, the user profile uid is a int etc.).

How can I debug the issue further and see, why the rebuild is failing?

More details below:

Full error message:

$ nixos-rebuild switch --show-trace
building Nix...
building the system configuration...
error: while evaluating the attribute 'config.system.build.toplevel' at /nix/var/nix/profiles/per-user/root/channels/nixos/nixos/modules/system/activation/top-level.nix:273:5:
while evaluating 'foldr' at /nix/var/nix/profiles/per-user/root/channels/nixos/lib/lists.nix:39:20, called from /nix/var/nix/profiles/per-user/root/channels/nixos/nixos/modules/system/activation/top-level.nix:137:12:
while evaluating 'fold'' at /nix/var/nix/profiles/per-user/root/channels/nixos/lib/lists.nix:42:15, called from /nix/var/nix/profiles/per-user/root/channels/nixos/lib/lists.nix:46:8:
while evaluating 'showWarnings' at /nix/var/nix/profiles/per-user/root/channels/nixos/nixos/modules/system/activation/top-level.nix:100:18, called from /nix/var/nix/profiles/per-user/root/channels/nixos/nixos/modules/system/activation/top-level.nix:107:16:
while evaluating 'foldr' at /nix/var/nix/profiles/per-user/root/channels/nixos/lib/lists.nix:39:20, called from /nix/var/nix/profiles/per-user/root/channels/nixos/nixos/modules/system/activation/top-level.nix:100:23:
while evaluating 'fold'' at /nix/var/nix/profiles/per-user/root/channels/nixos/lib/lists.nix:42:15, called from /nix/var/nix/profiles/per-user/root/channels/nixos/lib/lists.nix:46:8:
while evaluating anonymous function at /nix/var/nix/profiles/per-user/root/channels/nixos/nixos/modules/system/activation/top-level.nix:98:40, called from undefined position:
while evaluating the attribute 'assertion' at /nix/var/nix/profiles/per-user/root/channels/nixos/nixos/modules/config/users-groups.nix:570:9:
while evaluating 'idsAreUnique' at /nix/var/nix/profiles/per-user/root/channels/nixos/nixos/modules/config/users-groups.nix:369:23, called from /nix/var/nix/profiles/per-user/root/channels/nixos/nixos/modules/config/users-groups.nix:379:19:
while evaluating 'foldr' at /nix/var/nix/profiles/per-user/root/channels/nixos/lib/lists.nix:39:20, called from /nix/var/nix/profiles/per-user/root/channels/nixos/nixos/modules/config/users-groups.nix:369:33:
while evaluating 'fold'' at /nix/var/nix/profiles/per-user/root/channels/nixos/lib/lists.nix:42:15, called from /nix/var/nix/profiles/per-user/root/channels/nixos/lib/lists.nix:46:8:
while evaluating 'filterAttrs' at /nix/var/nix/profiles/per-user/root/channels/nixos/lib/attrsets.nix:115:23, called from /nix/var/nix/profiles/per-user/root/channels/nixos/nixos/modules/config/users-groups.nix:379:33:
while evaluating the attribute 'users' at undefined position:
while evaluating anonymous function at /nix/var/nix/profiles/per-user/root/channels/nixos/lib/modules.nix:75:45, called from undefined position:
while evaluating the attribute 'value' at /nix/var/nix/profiles/per-user/root/channels/nixos/lib/modules.nix:336:9:
while evaluating the option `users.users':
while evaluating the attribute 'isDefined' at /nix/var/nix/profiles/per-user/root/channels/nixos/lib/modules.nix:374:5:
while evaluating the attribute 'values' at /nix/var/nix/profiles/per-user/root/channels/nixos/lib/modules.nix:362:9:
while evaluating the attribute 'values' at /nix/var/nix/profiles/per-user/root/channels/nixos/lib/modules.nix:458:7:
while evaluating anonymous function at /nix/var/nix/profiles/per-user/root/channels/nixos/lib/modules.nix:348:28, called from /nix/var/nix/profiles/per-user/root/channels/nixos/lib/modules.nix:348:17:
while evaluating 'dischargeProperties' at /nix/var/nix/profiles/per-user/root/channels/nixos/lib/modules.nix:416:25, called from /nix/var/nix/profiles/per-user/root/channels/nixos/lib/modules.nix:349:62:
while evaluating the attribute 'value' at /nix/var/nix/profiles/per-user/root/channels/nixos/lib/modules.nix:232:58:
while evaluating the attribute 'config.users' at /root/config-testing/nixos-config/nixos-config/users-configuration.nix:7:3:
while evaluating the attribute 'nixosConfig.user.name' at undefined position:
while evaluating anonymous function at /nix/var/nix/profiles/per-user/root/channels/nixos/lib/modules.nix:75:45, called from undefined position:
while evaluating 'isType' at /nix/var/nix/profiles/per-user/root/channels/nixos/lib/types.nix:14:18, called from /nix/var/nix/profiles/per-user/root/channels/nixos/lib/modules.nix:76:14:
while evaluating the attribute '_type' at undefined position:
while evaluating anonymous function at /nix/var/nix/profiles/per-user/root/channels/nixos/lib/modules.nix:235:39, called from undefined position:
while evaluating 'mergeModules'' at /nix/var/nix/profiles/per-user/root/channels/nixos/lib/modules.nix:194:36, called from /nix/var/nix/profiles/per-user/root/channels/nixos/lib/modules.nix:253:13:
while evaluating 'flip' at /nix/var/nix/profiles/per-user/root/channels/nixos/lib/trivial.nix:101:16, called from /nix/var/nix/profiles/per-user/root/channels/nixos/lib/modules.nix:235:6:
while evaluating 'byName' at /nix/var/nix/profiles/per-user/root/channels/nixos/lib/modules.nix:217:25, called from /nix/var/nix/profiles/per-user/root/channels/nixos/lib/modules.nix:223:21:
while evaluating anonymous function at /nix/var/nix/profiles/per-user/root/channels/nixos/lib/modules.nix:217:47, called from /nix/var/nix/profiles/per-user/root/channels/nixos/lib/modules.nix:217:34:
value is a string while a set was expected, at /nix/var/nix/profiles/per-user/root/channels/nixos/lib/modules.nix:220:18

Extract from module options (file nixosConfig-options.nix):

{ pkgs, ... }:

with pkgs.lib;
let
  user = types.submodule {
    options = {
      uid = mkOption {
        type = types.int;
        description = "";
      };
      name = mkOption {
        type = types.string;
        description = "";
      };
      description = mkOption {
        type = types.string;
        description = "";
      };
      group = mkOption {
        type = types.string;
        description = "";
      };
      extraGroups = mkOption {
        type = types.listOf types.string;
        description = "";
      };
    };
  };

<other defintions are placed here>

in
{
  options.nixosConfig = with pkgs.lib; {
    user = user;

   <other defintions are placed here>
  };
}

Extract from module definition file (nixosConfig.nix):

{ config, pkgs, ... }:

with pkgs.lib;
let
  cfg = config.nixosConfig;
in
{
  imports = [
    # Option declarations
    ./nixosConfig-options.nix
    
    # Configuration 
    ./boot-configuration.nix
    <other nix files>
    ./users-configuration.nix
  ];
}

Extract from users definition file (users-configuration.nix):

{ config, pkgs, ... }:

{
  users.users."${config.nixosConfig.user.name}" = {
    uid = config.nixosConfig.user.uid;
    description = config.nixosConfig.user.description;
    
    group = config.nixosConfig.user.group;
    extraGroups = config.nixosConfig.user.extraGroups;

    createHome = true;
    isNormalUser = true;
    home = "/home/${config.nixosConfig.user.name}";
  };
}

Extract from configuration.nix:

{ config, pkgs, ... }:

let
  nixosConfig = import ./machineprofile.nix { pkgs=pkgs; };
in
{
  imports = [
    # Hardware configuration, pre-generated
    /etc/nixos/hardware-configuration.nix
    # NixosConfig
    ./nixos-config/nixosConfig.nix
  ];

  nixosConfig = nixosConfig;
  system.stateVersion = "18.09";
}

There is a lot here, I recommend simplifying things to debug your problem.

One issue I see is that submodules are not options but it looks like you are treating them as options. Submodules are types for options to have, so you need to be making a option that has the type of your user submodule.

1 Like