refactor: abstract app user creation via custom options

This commit is contained in:
Tibo De Peuter 2026-07-17 23:26:03 +02:00
parent 1403b1f9c0
commit d742671460
Signed by: tdpeuter
SSH key fingerprint: SHA256:u/h/LVoqKF1Iz02uOyxe6hcjmoZASCGV2HM0TG9ZMoU
7 changed files with 52 additions and 36 deletions

View file

@ -95,22 +95,17 @@ in {
};
# Create a user for each app.
users.users = let
homelab.appUsers = let
mkUser = uid: {
uid = lib.mkForce uid;
isSystemUser = true;
inherit uid;
group = config.users.groups.media.name;
home = "/var/empty";
shell = null;
};
in {
bazarr = lib.mkIf cfg.bazarr.enable (mkUser 3003);
prowlarr = lib.mkIf cfg.prowlarr.enable (mkUser 3004);
qbittorrent = lib.mkIf cfg.qbittorrent.enable (mkUser 3005) // {
extraGroups = [
config.users.groups.apps.name
];
};
qbittorrent = lib.mkIf cfg.qbittorrent.enable ((mkUser 3005) // {
extraGroups = [ config.users.groups.apps.name ];
});
radarr = lib.mkIf cfg.radarr.enable (mkUser 3006);
sonarr = lib.mkIf cfg.sonarr.enable (mkUser 3007);
};

View file

@ -34,12 +34,9 @@ in {
virtualisation.containers.enable = true;
};
users.users.calibre = {
uid = lib.mkForce 3010;
isSystemUser = true;
homelab.appUsers.calibre = {
uid = 3010;
group = config.users.groups.media.name;
home = "/var/empty";
shell = null;
};
homelab.nfsMounts."${books}" = {

View file

@ -30,12 +30,8 @@ in {
virtualisation.containers.enable = true;
};
users.users.gitea = {
uid = lib.mkForce UID;
isSystemUser = true;
group = config.users.groups.apps.name;
home = "/var/empty";
shell = null;
homelab.appUsers.gitea = {
uid = UID;
};
# Use filesystem mounts because rootless containers otherwise don't have access to the mount path (nested in docker directories).

View file

@ -26,12 +26,8 @@ in {
virtualisation.containers.enable = true;
};
users.users.homepage = {
uid = lib.mkForce 3018;
isSystemUser = true;
group = config.users.groups.apps.name;
home = "/var/empty";
shell = null;
homelab.appUsers.homepage = {
uid = 3018;
};
homelab.nfsMounts."${homepage-config}" = {

View file

@ -36,15 +36,9 @@ in {
"/srv/photo" = mkMount "192.168.0.11:/mnt/BIG/MEDIA/PHOTO/ARCHIVE";
};
users.users.jellyfin = {
uid = lib.mkForce UID;
isSystemUser = true;
group = config.users.groups.apps.name;
extraGroups = [
config.users.groups.media.name
];
home = "/var/empty";
shell = null;
homelab.appUsers.jellyfin = {
uid = UID;
extraGroups = [ config.users.groups.media.name ];
};
# Make sure the Docker network exists.

View file

@ -3,6 +3,7 @@
./docker.nix
./monitoring.nix
./nfs.nix
./users.nix
];
config = {

37
modules/common/users.nix Normal file
View file

@ -0,0 +1,37 @@
{ config, lib, ... }:
{
options.homelab.appUsers = lib.mkOption {
type = lib.types.attrsOf (lib.types.submodule {
options = {
uid = lib.mkOption {
type = lib.types.int;
description = "The user ID for the app user";
};
group = lib.mkOption {
type = lib.types.str;
default = config.users.groups.apps.name;
description = "The primary group for the app user";
};
extraGroups = lib.mkOption {
type = lib.types.listOf lib.types.str;
default = [];
description = "Extra groups for the app user";
};
};
});
default = {};
description = "App users to automatically create with standard homelab options";
};
config = {
users.users = lib.mapAttrs (name: cfg: {
uid = lib.mkForce cfg.uid;
isSystemUser = true;
group = cfg.group;
extraGroups = cfg.extraGroups;
home = "/var/empty";
shell = null;
}) config.homelab.appUsers;
};
}