selfhostblocks/modules/blocks/davfs.nix
2026-06-25 22:33:29 +02:00

125 lines
3.8 KiB
Nix

{ config, lib, ... }:
let
cfg = config.shb.davfs;
in
{
options.shb.davfs = {
mounts = lib.mkOption {
description = "List of mounts.";
default = [ ];
type = lib.types.listOf (
lib.types.submodule {
options = {
remoteUrl = lib.mkOption {
type = lib.types.str;
description = "Webdav endpoint to connect to.";
example = "https://my.domain.com/dav";
};
mountPoint = lib.mkOption {
type = lib.types.str;
description = "Mount point to mount the webdav endpoint on.";
example = "/mnt";
};
username = lib.mkOption {
type = lib.types.str;
description = "Username to connect to the webdav endpoint.";
};
passwordFile = lib.mkOption {
type = lib.types.str;
description = "Password to connect to the webdav endpoint.";
};
uid = lib.mkOption {
type = lib.types.nullOr lib.types.int;
description = "User owner of the mount point.";
example = 1000;
default = null;
};
gid = lib.mkOption {
type = lib.types.nullOr lib.types.int;
description = "Group owner of the mount point.";
example = 1000;
default = null;
};
fileMode = lib.mkOption {
type = lib.types.nullOr lib.types.str;
description = "File creation mode";
example = "0664";
default = null;
};
directoryMode = lib.mkOption {
type = lib.types.nullOr lib.types.str;
description = "Directory creation mode";
example = "2775";
default = null;
};
automount = lib.mkOption {
type = lib.types.bool;
description = "Create a systemd automount unit";
default = true;
};
};
}
);
};
};
config = {
services.davfs2.enable = builtins.length cfg.mounts > 0;
systemd.services = lib.optionalAttrs (builtins.length cfg.mounts > 0) {
davfs2-password-generation = {
wantedBy = [ "multi-user.target" ];
serviceConfig.Type = "oneshot";
script = ''
mkdir -p /etc/davfs2
[ -f /etc/davfs2/secrets ] && mv /etc/davfs2/secrets /etc/davfs2/secrets.prev
touch /etc/davfs2/secrets
chown root: /etc/davfs2
chown root: /etc/davfs2/secrets
chmod 700 /etc/davfs2
chmod 600 /etc/davfs2/secrets
''
+ (
let
mkPasswordCmd = cfg': ''
printf "%s %s %s\n" "${cfg'.mountPoint}" "${cfg'.username}" "$(cat ${cfg'.passwordFile})" >> /etc/davfs2/secrets
'';
in
lib.concatStringsSep "\n" (map mkPasswordCmd cfg.mounts)
);
};
};
systemd.mounts =
let
mkMountCfg = c: {
enable = true;
description = "Webdav mount point";
after = [ "network-online.target" ];
wants = [ "network-online.target" ];
wantedBy = [ "multi-user.target" ];
what = c.remoteUrl;
where = c.mountPoint;
options = lib.concatStringsSep "," (
(lib.optional (!(isNull c.uid)) "uid=${toString c.uid}")
++ (lib.optional (!(isNull c.gid)) "gid=${toString c.gid}")
++ (lib.optional (!(isNull c.fileMode)) "file_mode=${toString c.fileMode}")
++ (lib.optional (!(isNull c.directoryMode)) "dir_mode=${toString c.directoryMode}")
);
type = "davfs";
mountConfig.TimeoutSec = 15;
};
in
map mkMountCfg cfg.mounts;
};
}