diff --git a/configs/nixos/lib/copyparty-config.nix b/configs/nixos/lib/copyparty-config.nix new file mode 100644 index 0000000..4c3a52a --- /dev/null +++ b/configs/nixos/lib/copyparty-config.nix @@ -0,0 +1,65 @@ +{ + lib, + pkgs, +}: +cfg: +let + renderedReverseProxy = lib.optionalString cfg.reverseProxy.enable '' + rproxy: 1 + xff-hdr: x-forwarded-for + ${lib.concatStringsSep "\n" (map renderTrustedSource cfg.reverseProxy.trustedSources)} + ''; + + renderTrustedSource = source: " xff-src: ${source}"; + + renderedVolumes = lib.concatStringsSep "\n" (lib.mapAttrsToList renderVolume cfg.volumes); + + renderVolume = urlPath: volume: '' + [${urlPath}] + ${volume.path} + accs: + ${lib.concatStringsSep "\n" (lib.mapAttrsToList renderAccess volume.access)} + ${lib.optionalString (volume.flags != [ ]) '' + flags: + ${renderFlags volume.flags} + ''} + ''; + + renderAccess = permission: accounts: " ${permission}: ${lib.concatStringsSep ", " accounts}"; + + renderFlags = + flags: lib.concatMapStringsSep "\n" (flag: " ${flag}") (flags ++ defaultPermissionFlags flags); + + defaultPermissionFlags = + flags: + lib.optionals (!(hasFlag "chmod_d" flags)) [ "chmod_d: 2770" ] + ++ lib.optionals (!(hasFlag "chmod_f" flags)) [ "chmod_f: 660" ]; + + hasFlag = + name: flags: lib.any (flag: lib.hasPrefix "${name}:" flag || lib.hasPrefix "${name}=" flag) flags; +in +pkgs.writeShellScript "copyparty-config" '' + set -eu + + umask 077 + password="$(${lib.getExe' pkgs.coreutils "tr"} -d '\n' < "$CREDENTIALS_DIRECTORY/password")" + + cat > /run/copyparty/copyparty.conf < /run/copyparty/copyparty.conf <