From 47ad89dee1995b1578821dec7e82368db82f7b98 Mon Sep 17 00:00:00 2001 From: Rodrigo Arias Mallo Date: Fri, 13 Jun 2025 12:53:58 +0200 Subject: [PATCH] Add p service for pastes MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Reviewed-by: Aleix Boné Reviewed-by: Aleix Roca Nonell --- m/module/p.nix | 68 ++++++++++++++++++++++++++++++++++++++++ m/tent/configuration.nix | 3 ++ m/tent/nginx.nix | 3 ++ 3 files changed, 74 insertions(+) create mode 100644 m/module/p.nix diff --git a/m/module/p.nix b/m/module/p.nix new file mode 100644 index 00000000..2005eb87 --- /dev/null +++ b/m/module/p.nix @@ -0,0 +1,68 @@ +{ config, lib, pkgs, ... }: + +let + cfg = config.services.p; +in +{ + options = { + services.p = { + enable = lib.mkOption { + type = lib.types.bool; + default = false; + description = "Whether to enable the p service."; + }; + path = lib.mkOption { + type = lib.types.str; + default = "/var/lib/p"; + description = "Where to save the pasted files on disk."; + }; + url = lib.mkOption { + type = lib.types.str; + default = "https://jungle.bsc.es/p"; + description = "URL prefix for the printed file."; + }; + }; + }; + + config = lib.mkIf cfg.enable { + environment.systemPackages = let + p = pkgs.writeShellScriptBin "p" '' + set -e + pastedir="${cfg.path}/$USER" + cd "$pastedir" + + ext="txt" + if [ -n "$1" ]; then + ext="$1" + fi + + out=$(mktemp "XXXXXXXX.$ext") + cat > "$out" + chmod go+r "$out" + echo "${cfg.url}/$USER/$out" + ''; + in [ p ]; + + systemd.services.p = let + # Take only normal users + users = lib.filterAttrs (_: v: v.isNormalUser) config.users.users; + # Create a directory for each user + commands = lib.concatLists (lib.mapAttrsToList (_: user: [ + "install -d -o ${user.name} -g ${user.group} -m 0755 ${cfg.path}/${user.name}" + ]) users); + in { + description = "P service setup"; + requires = [ "network-online.target" ]; + #wants = [ "remote-fs.target" ]; + #after = [ "remote-fs.target" ]; + wantedBy = [ "multi-user.target" ]; + serviceConfig = { + ExecStart = pkgs.writeShellScript "p-init.sh" ('' + + install -d -o root -g root -m 0755 ${cfg.path} + + '' + (lib.concatLines commands)); + }; + }; + }; +} diff --git a/m/tent/configuration.nix b/m/tent/configuration.nix index bf18f911..327d4d46 100644 --- a/m/tent/configuration.nix +++ b/m/tent/configuration.nix @@ -12,6 +12,7 @@ ./gitlab-runner.nix ./gitea.nix ../hut/public-inbox.nix + ../module/p.nix ]; # Select the this using the ID to avoid mismatches @@ -32,6 +33,8 @@ defaultGateway = "10.0.44.1"; }; + services.p.enable = true; + services.prometheus.exporters.node = { enable = true; enabledCollectors = [ "systemd" ]; diff --git a/m/tent/nginx.nix b/m/tent/nginx.nix index 0e6811af..de9214e7 100644 --- a/m/tent/nginx.nix +++ b/m/tent/nginx.nix @@ -64,6 +64,9 @@ in autoindex on; absolute_redirect off; } + location /p/ { + alias /var/lib/p/; + } ''; }; };