about summary refs log tree commit diff
path: root/users
diff options
context:
space:
mode:
authorsterni <sternenseemann@systemli.org>2024-12-07T00·30+0100
committerclbot <clbot@tvl.fyi>2024-12-07T01·06+0000
commit8f134350205962f5d6a550e6a20957bed62bdad9 (patch)
treea4aa271e3266ea23fa80e59434d23bd7f97da178 /users
parent317bd54038d6b68b39ce27d946892ebe4bda2f8c (diff)
feat(sterni/nix/build): check in toPlainText r/8988
This is a little helper which tries to render different input files into
a fancier, human readable plain text form. This is quite useful to
gether with build.gopherHole.

Change-Id: Ibe0050fa6a55e85745127a287bba0febeeb75849
Reviewed-on: https://cl.tvl.fyi/c/depot/+/12874
Reviewed-by: sterni <sternenseemann@systemli.org>
Tested-by: BuildkiteCI
Autosubmit: sterni <sternenseemann@systemli.org>
Diffstat (limited to 'users')
-rw-r--r--users/sterni/nix/build/toPlainText/default.nix56
1 files changed, 56 insertions, 0 deletions
diff --git a/users/sterni/nix/build/toPlainText/default.nix b/users/sterni/nix/build/toPlainText/default.nix
new file mode 100644
index 000000000000..c59e372d3b50
--- /dev/null
+++ b/users/sterni/nix/build/toPlainText/default.nix
@@ -0,0 +1,56 @@
+{ pkgs, depot, lib, ... }:
+
+{ fmt ? null
+, passthru ? { }
+, ...
+}@args:
+
+let
+  inherit (depot.nix) getBins utils;
+
+  bins = getBins pkgs.lowdown [
+    "lowdown"
+  ] // getBins pkgs.mandoc [
+    "mandoc"
+  ] // getBins pkgs.gnused [
+    "sed"
+  ];
+
+  actions = {
+    mdoc = ''
+      ${bins.mandoc} -T utf8 "${thing}" > "$out"
+    '';
+
+    # Use lowdown's terminal target, but strip all ANSI SGR escape sequences
+    markdown = ''
+      ${bins.lowdown} -Tterm "${thing}" \
+      | ${bins.sed} -e 's|\\x1b\\[[;0-9]*m||g' \
+      > "$out"
+    '';
+
+    plain = ''
+      cp --reflink=auto "${thing}" "$out"
+    '';
+  };
+
+  thingType = builtins.head (
+    builtins.filter (x: x != null) (
+      builtins.map (t: if args ? ${t} then t else null) (
+        builtins.attrNames actions
+      )
+    )
+  );
+
+  thing = args.${thingType};
+  name = args.name or utils.storePathName thing;
+in
+
+pkgs.runCommand name
+{
+  inherit passthru;
+}
+  (
+    actions.${thingType} + lib.optionalString (fmt != null) ''
+      fmt -w "${lib.escapeShellArg (toString fmt)}" "$out"
+    ''
+  )