[Nix-dev] Wrapping functions?

Nicolas Pierron nicolas.b.pierron at gmail.com
Fri Mar 27 21:34:07 CET 2015


Yes, there is a better way to do this, by extending the sub-modules by
the extraUsers option, in a similar way as done by sshd [1,2].  The
idea is that instead of making a function which will pre-process all
the options definitions of one user, you will just extend all users
with a flag.

Note that `userOptions` in sshd is a sub-module, which means that it
behaves as a module in NixOS, except that its `config` argument is re
stricted to what is available for each instance of the option (read
for each user).

Basically, the code that you had in your gist should look something
like (for mkUser):


{config, pkgs, lib, ...}:

with lib;
let topConfig = config; in

{
  options.users.extraUsers = mkOption {
    options = {config, name, ...}: {
      options = {
        isUser = mkOption {
          default = false;
          type = types.bool;
          description = "set defaults for one user";
        };
      };

      config = mkIf config.isUser {
       extraGroups     = [ "users" ];
       group           = name;
       home            = "/home/${name}";
       createHome      = true;
       useDefaultShell = false;
       shell           = "/run/current-system/sw/bin/bash";
      };
    };
  };

}


This way, any user declaration would look like

{lib, ...}:

with lib;

{
  config = {
    users.extraUsers.joe = {
      isUser = true;
    };
  }
}


[1] https://github.com/NixOS/nixpkgs/blob/7f90cc40b4b62e9495a77121dc96278501afd79b/nixos/modules/services/networking/ssh/sshd.nix#L282-L284
[2] https://github.com/NixOS/nixpkgs/blob/7f90cc40b4b62e9495a77121dc96278501afd79b/nixos/modules/services/networking/ssh/sshd.nix#L26-L54

On Fri, Mar 27, 2015 at 5:15 PM, Joe Hillenbrand <joehillen at gmail.com> wrote:
> Is there a better way to do this?
> https://gist.github.com/joehillen/5e762c74b6b8f7595736
>
> What I'm trying to do here is create wrapper functions that set some default
> values.
> The problem is that I have to pass along every attribute I want to be able
> to set at the top level.
> As you can see, I don't do anything with `description` or `openssh`. I just
> pass it along all the way
> down to `extraUsers`.
>
> Based on what I've seen, there seems like there is some way to accomplish
> this (like an override or 'mkDefault')
> but I keep getting confused.
>
> Still learning. Please excuse my ignorance.
>
> -Joe Hillenbrand
>
>
> _______________________________________________
> nix-dev mailing list
> nix-dev at lists.science.uu.nl
> http://lists.science.uu.nl/mailman/listinfo/nix-dev
>



-- 
Nicolas Pierron
http://www.linkedin.com/in/nicolasbpierron - http://nbp.name/


More information about the nix-dev mailing list