nixfiles/nixos/modules/user.nix

107 lines
3.8 KiB
Nix
Raw Normal View History

{ lib, options, config, ... }:
let
2022-09-09 01:04:25 +01:00
inherit (lib) mkIf mkDefault mkOption mkMerge mkAliasDefinitions optional;
2022-05-28 19:02:13 +01:00
inherit (lib.my) mkBoolOpt' mkOpt' mkDefault';
cfg = config.my.user;
user' = cfg.config;
2022-03-26 14:20:30 +00:00
user = config.users.users.${user'.name};
in
{
options.my.user = with lib.types; {
enable = mkBoolOpt' true "Whether to create a primary user.";
2022-05-28 19:02:13 +01:00
passwordSecret = mkOpt' (nullOr str) "user-passwd.txt" "Name of user password secret.";
2022-09-08 20:31:44 +01:00
tmphome = mkBoolOpt' true "Whether to persist home directory files under tmproot";
config = mkOption {
type = options.users.users.type.nestedTypes.elemType;
default = { };
description = "User definition (as `users.users.*`).";
};
homeConfig = mkOption {
type = options.home-manager.users.type.nestedTypes.elemType;
default = { };
# Prevent docs traversing into all of home-manager
visible = "shallow";
description = "Home configuration (as `home-manager.users.*`)";
};
};
2022-05-28 19:02:13 +01:00
config = mkIf cfg.enable (mkMerge [
{
my = {
user = {
config = {
name = mkDefault' "dev";
isNormalUser = true;
uid = mkDefault 1000;
2022-09-09 01:04:25 +01:00
extraGroups =
[ "wheel" "kvm" ] ++
(optional config.networking.networkmanager.enable "networkmanager");
2022-05-28 19:02:13 +01:00
password = mkIf (cfg.passwordSecret == null) (mkDefault "hunter2");
shell =
let shell = cfg.homeConfig.my.shell;
in mkIf (shell != null) (mkDefault' shell);
2023-11-02 13:41:50 +00:00
openssh.authorizedKeys.keyFiles = [ lib.my.c.sshKeyFiles.me ];
2022-05-28 19:02:13 +01:00
};
homeConfig = {
# In order for this option to evaluate on its own, home-manager expects the `name` (which is derived from the
# parent attr name) to be the users name, aka `home-manager.users.<name>`
_module.args.name = lib.mkForce user'.name;
};
2022-03-26 14:20:30 +00:00
};
2022-09-08 20:31:44 +01:00
tmproot = mkIf cfg.tmphome {
2022-05-28 19:02:13 +01:00
unsaved.ignore = [
# Auto-generated (on activation?)
"/home/${user'.name}/.nix-profile"
"/home/${user'.name}/.nix-defexpr"
2022-05-28 13:57:01 +01:00
2022-05-28 19:02:13 +01:00
"/home/${user'.name}/.config/fish/fish_variables"
2022-05-28 13:57:01 +01:00
];
2022-05-28 19:02:13 +01:00
persistence.config =
let
perms = {
mode = "0700";
user = user.name;
group = user.group;
};
in
{
files = (map (file: {
2022-05-28 19:02:13 +01:00
inherit file;
parentDirectory = perms;
}) [
"/home/${user'.name}/.bash_history"
"/home/${user'.name}/.lesshst"
]) ++ [
# Just to make sure we get correct default perms
"/home/.tmproot.dummy"
2022-05-28 19:02:13 +01:00
];
directories = map (directory: {
inherit directory;
} // perms) [
# Persist all of fish; it's not easy to persist just the history fish won't let you move it to a different
# directory. Also it does some funny stuff and can't really be a symlink it seems.
"/home/${user'.name}/.local/share/fish"
2022-06-17 23:15:39 +01:00
"/home/${user'.name}/.cache/nix"
2022-05-28 19:02:13 +01:00
];
};
2022-05-28 13:57:01 +01:00
};
};
2022-05-28 19:02:13 +01:00
# mkAliasDefinitions will copy the unmerged defintions to allow the upstream submodule to deal with
users.users.${user'.name} = mkAliasDefinitions options.my.user.config;
2022-05-28 19:02:13 +01:00
# NOTE: As the "outermost" module is still being evaluated in NixOS land, special params (e.g. pkgs) won't be
# passed to it
home-manager.users.${user'.name} = mkAliasDefinitions options.my.user.homeConfig;
}
(mkIf (cfg.passwordSecret != null) {
my = {
secrets.files."${cfg.passwordSecret}" = {};
user.config.hashedPasswordFile = config.age.secrets."${cfg.passwordSecret}".path;
2022-05-28 19:02:13 +01:00
};
})
]);
}