From 65f2f81a8146894768ab6136fb31212d485881f1 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 --- m/module/p.nix | 68 +++++++++++++++++++++++++++++++++++++ m/tent/configuration.nix | 3 ++ m/tent/nginx.nix | 3 ++ web/content/paste/_index.md | 16 ++++----- 4 files changed, 82 insertions(+), 8 deletions(-) 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/; + } ''; }; }; diff --git a/web/content/paste/_index.md b/web/content/paste/_index.md index e4ebd61d..1022a31d 100644 --- a/web/content/paste/_index.md +++ b/web/content/paste/_index.md @@ -5,13 +5,13 @@ author: "Rodrigo Arias Mallo" date: 2024-09-20 --- -The hut machine provides a paste service using the program `p` (as in paste). +The tent machine provides a paste service using the program `p` (as in paste). -You can use it directly from the hut machine or remotely if you have [SSH -access](/access) to hut using the following alias: +You can use it directly from the tent machine or remotely if you have [SSH +access](/access) to tent using the following alias: ``` -alias p="ssh hut p" +alias p="ssh tent p" ``` You can add it to bashrc or zshrc for persistent installation. @@ -19,7 +19,7 @@ You can add it to bashrc or zshrc for persistent installation. ## Usage The `p` command reads from the standard input, uploads the content to a file -in the ceph filesystem and prints the URL to access it. It only accepts an +in the local filesystem and prints the URL to access it. It only accepts an optional argument, which is the extension of the file that will be stored on disk (without the dot). By default it uses the `txt` extension, so plain text can be read in the browser directly. @@ -28,21 +28,21 @@ can be read in the browser directly. p [extension] ``` -To remove files, go to `/ceph/p/$USER` and remove them manually. +To remove files, go to `/var/lib/p/$USER` and remove them manually. ## Examples Share a text file, in this case the source of p itself: ``` -hut% p < m/hut/p.nix +tent% p < m/tent/p.nix https://jungle.bsc.es/p/rarias/okbtG130.txt ``` Paste the last dmesg lines directly from a pipe: ``` -hut% dmesg | tail -5 | p +tent% dmesg | tail -5 | p https://jungle.bsc.es/p/rarias/luX4STm9.txt ```