2023-01-30 10:34:36 +00:00
|
|
|
{ config, lib, options, sane-lib, ... }:
|
2023-01-30 08:32:55 +00:00
|
|
|
|
|
|
|
let
|
2023-01-30 08:53:40 +00:00
|
|
|
inherit (builtins) attrValues;
|
|
|
|
inherit (lib) count mapAttrs' mapAttrsToList mkIf mkMerge mkOption types;
|
|
|
|
sane-user-cfg = config.sane.user;
|
2023-01-30 08:32:55 +00:00
|
|
|
cfg = config.sane.users;
|
|
|
|
path-lib = sane-lib.path;
|
2023-01-30 08:53:40 +00:00
|
|
|
userOptions = {
|
2023-01-30 08:32:55 +00:00
|
|
|
options = {
|
|
|
|
fs = mkOption {
|
|
|
|
type = types.attrs;
|
2023-01-30 08:53:40 +00:00
|
|
|
default = {};
|
2023-01-30 08:32:55 +00:00
|
|
|
description = ''
|
|
|
|
entries to pass onto `sane.fs` after prepending the user's home-dir to the path.
|
|
|
|
e.g. `sane.users.colin.fs."/.config/aerc" = X`
|
|
|
|
=> `sane.fs."/home/colin/.config/aerc" = X;
|
|
|
|
'';
|
|
|
|
};
|
2023-01-30 10:34:36 +00:00
|
|
|
|
|
|
|
persist = mkOption {
|
2023-01-30 10:48:32 +00:00
|
|
|
type = options.sane.persist.sys.type;
|
2023-01-30 10:34:36 +00:00
|
|
|
default = {};
|
|
|
|
description = ''
|
2023-01-30 10:48:32 +00:00
|
|
|
entries to pass onto `sane.persist.sys` after prepending the user's home-dir to the path.
|
2023-01-30 10:34:36 +00:00
|
|
|
'';
|
|
|
|
};
|
2023-01-30 08:32:55 +00:00
|
|
|
};
|
|
|
|
};
|
2023-01-30 11:06:47 +00:00
|
|
|
userModule = types.submodule ({ name, config, ... }: {
|
2023-01-30 09:13:43 +00:00
|
|
|
options = userOptions.options // {
|
2023-01-30 08:53:40 +00:00
|
|
|
default = mkOption {
|
|
|
|
type = types.bool;
|
|
|
|
default = false;
|
|
|
|
description = ''
|
|
|
|
only one default user may exist.
|
|
|
|
this option determines what the `sane.user` shorthand evaluates to.
|
|
|
|
'';
|
|
|
|
};
|
2023-01-30 11:06:47 +00:00
|
|
|
|
|
|
|
home = mkOption {
|
|
|
|
type = types.str;
|
|
|
|
# XXX: we'd prefer to set this to `config.users.users.home`, but that causes infinite recursion...
|
|
|
|
# TODO: maybe assert that this matches the actual home?
|
|
|
|
default = "/home/${name}";
|
|
|
|
};
|
2023-01-30 08:53:40 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
# if we're the default user, inherit whatever settings were routed to the default user
|
|
|
|
config = mkIf config.default sane-user-cfg;
|
|
|
|
});
|
2023-01-30 10:48:32 +00:00
|
|
|
processUser = user: defn:
|
|
|
|
let
|
|
|
|
prefixWithHome = mapAttrs' (path: value: {
|
2023-01-30 11:06:47 +00:00
|
|
|
name = path-lib.concat [ defn.home path ];
|
2023-01-30 10:48:32 +00:00
|
|
|
inherit value;
|
|
|
|
});
|
|
|
|
in
|
|
|
|
{
|
|
|
|
sane.fs = prefixWithHome defn.fs;
|
|
|
|
|
|
|
|
# `byPath` is the actual output here, computed from the other keys.
|
|
|
|
sane.persist.sys.byPath = prefixWithHome defn.persist.byPath;
|
|
|
|
};
|
2023-01-30 08:32:55 +00:00
|
|
|
in
|
|
|
|
{
|
|
|
|
options = {
|
|
|
|
sane.users = mkOption {
|
|
|
|
type = types.attrsOf userModule;
|
|
|
|
default = {};
|
|
|
|
description = ''
|
|
|
|
options to apply to the given user.
|
|
|
|
the user is expected to be created externally.
|
|
|
|
configs applied at this level are simply transformed and then merged
|
|
|
|
into the toplevel `sane` options. it's merely a shorthand.
|
|
|
|
'';
|
|
|
|
};
|
2023-01-30 08:53:40 +00:00
|
|
|
|
|
|
|
sane.user = mkOption {
|
|
|
|
type = types.nullOr (types.submodule userOptions);
|
|
|
|
default = null;
|
|
|
|
description = ''
|
|
|
|
options to pass down to the default user
|
|
|
|
'';
|
|
|
|
};
|
2023-01-30 08:32:55 +00:00
|
|
|
};
|
|
|
|
config =
|
|
|
|
let
|
|
|
|
configs = mapAttrsToList processUser cfg;
|
2023-01-30 08:53:40 +00:00
|
|
|
num-default-users = count (u: u.default) (attrValues cfg);
|
2023-01-30 08:32:55 +00:00
|
|
|
take = f: {
|
|
|
|
sane.fs = f.sane.fs;
|
2023-01-30 10:48:32 +00:00
|
|
|
sane.persist.sys.byPath = f.sane.persist.sys.byPath;
|
2023-01-30 08:32:55 +00:00
|
|
|
};
|
2023-01-30 08:53:40 +00:00
|
|
|
in mkMerge [
|
|
|
|
(take (sane-lib.mkTypedMerge take configs))
|
|
|
|
{
|
|
|
|
assertions = [
|
|
|
|
{
|
|
|
|
assertion = sane-user-cfg == null || num-default-users != 0;
|
|
|
|
message = "cannot set `sane.user` without first setting `sane.users.<user>.default = true` for some user";
|
|
|
|
}
|
|
|
|
{
|
|
|
|
assertion = num-default-users <= 1;
|
|
|
|
message = "cannot set more than one default user";
|
|
|
|
}
|
|
|
|
];
|
|
|
|
}
|
|
|
|
];
|
2023-01-30 08:32:55 +00:00
|
|
|
}
|