about summary refs log tree commit diff
path: root/users/Profpatsch
diff options
context:
space:
mode:
authorProfpatsch <mail@profpatsch.de>2022-01-09T00·20+0100
committerProfpatsch <mail@profpatsch.de>2022-01-09T09·32+0000
commitfed41f49597d5f8e0501fa494b314e3fa04d0883 (patch)
tree409b57db65b4a1b16293b8cf5d0242df90b9fce1 /users/Profpatsch
parente1044c1559a01eee10bcd71122077dcdc2ea7e9a (diff)
feat(users/Profpatsch): set up stow for nix-home r/3577
nix-home is (hopefully) gonna be a home-manager alternative for my
home directory.

Files are symlinked into the home directory via GNU stow (since that
is a tried and tested tool), so first step is to set up the base code
for that.

Implements a small tool that reads a single environment variable and
prints it to stdout.

Change-Id: Ifa3fd9f9e1cedc52c3002196d3971b02cb840e80
Reviewed-on: https://cl.tvl.fyi/c/depot/+/4832
Tested-by: BuildkiteCI
Reviewed-by: Profpatsch <mail@profpatsch.de>
Autosubmit: Profpatsch <mail@profpatsch.de>
Diffstat (limited to 'users/Profpatsch')
-rw-r--r--users/Profpatsch/execline/default.nix21
-rw-r--r--users/Profpatsch/netstring/default.nix3
-rw-r--r--users/Profpatsch/nix-home/default.nix91
3 files changed, 115 insertions, 0 deletions
diff --git a/users/Profpatsch/execline/default.nix b/users/Profpatsch/execline/default.nix
index 1f75b97591..c6a8d284a6 100644
--- a/users/Profpatsch/execline/default.nix
+++ b/users/Profpatsch/execline/default.nix
@@ -5,8 +5,29 @@ let
     name = "exec-helpers";
   } (builtins.readFile ./exec_helpers.rs);
 
+  print-one-env = depot.nix.writers.rustSimple {
+    name = "print-one-env";
+    dependencies = [
+      depot.users.Profpatsch.execline.exec-helpers
+    ];
+  } ''
+    extern crate exec_helpers;
+    use std::os::unix::ffi::OsStrExt;
+    use std::io::Write;
+
+    fn main() {
+      let args = exec_helpers::args("print-one-env", 1);
+      let valname = std::ffi::OsStr::from_bytes(&args[0]);
+      match std::env::var_os(&valname) {
+        None => exec_helpers::die_user_error("print-one-env", format!("Env variable `{:?}` is not set", valname)),
+        Some(val) => std::io::stdout().write_all(&val.as_bytes()).unwrap()
+      }
+    }
+  '';
+
 in depot.nix.readTree.drvTargets {
   inherit
     exec-helpers
+    print-one-env
     ;
 }
diff --git a/users/Profpatsch/netstring/default.nix b/users/Profpatsch/netstring/default.nix
index dcc29d7e6f..b4990cae67 100644
--- a/users/Profpatsch/netstring/default.nix
+++ b/users/Profpatsch/netstring/default.nix
@@ -57,6 +57,9 @@ let
 
 in depot.nix.readTree.drvTargets {
   inherit
+    toNetstring
+    toNetstringList
+    toNetstringKeyVal
     python-netstring
     rust-netstring
       ;
diff --git a/users/Profpatsch/nix-home/default.nix b/users/Profpatsch/nix-home/default.nix
new file mode 100644
index 0000000000..2b44868f5a
--- /dev/null
+++ b/users/Profpatsch/nix-home/default.nix
@@ -0,0 +1,91 @@
+{ depot, pkgs, lib, ... }:
+
+let
+  bins = depot.nix.getBins pkgs.stow [ "stow" ]
+      // depot.nix.getBins pkgs.coreutils [ "mkdir" "ln" "printenv" "rm" ]
+      // depot.nix.getBins pkgs.xe [ "xe" ]
+      // depot.nix.getBins pkgs.lr [ "lr" ]
+      ;
+
+  # run stow to populate the target directory with the given stow package, read from stowDir.
+  # Bear in mind that `stowDirOriginPath` should always be semantically bound to the given `stowDir`, otherwise stow might become rather confused.
+  runStow = {
+    # “stow package” to stow (see manpage)
+    stowPackage,
+    # “target directory” to stow in (see manpage)
+    targetDir,
+    # The “stow directory” (see manpage), containing “stow packages” (see manpage)
+    stowDir,
+    # representative directory for the stowDir in the file system, against which stow will create relative links.
+    # ATTN: this is always overwritten with the contents of `stowDir`! You shouldn’t re-use the same `stowDirOriginPath` for different `stowDir`s, otherwise there might be surprises.
+    stowDirOriginPath,
+  }: depot.nix.writeExecline "stow-${stowPackage}" {} [
+    # first, create a temporary stow directory to use as source
+    # (stow will use it to determine the origin of files)
+    "if" [ bins.mkdir "-p" stowDirOriginPath ]
+    # remove old symlinks
+    "if" [
+      "pipeline" [
+        bins.lr "-0" "-t" "depth == 1 && type == l" stowDirOriginPath
+      ]
+      bins.xe "-0" bins.rm
+    ]
+    # populate with new stow targets
+    "if" [
+      "elglob" "-w0" "stowPackages" "${stowDir}/*"
+      bins.ln "--force" "-st" stowDirOriginPath "$stowPackages"
+    ]
+    # stow always looks for $HOME/.stowrc to read more arguments
+    "export" "HOME" "/homeless-shelter"
+    bins.stow
+      # always run restow for now; this does more stat but will remove stale links
+      "--restow"
+      "--dir" stowDirOriginPath
+      "--target" targetDir
+      stowPackage
+  ];
+
+  # create a stow dir from a list of drv paths and a stow package name.
+  makeStowDir =
+    (with depot.nix.yants;
+     defun
+       [ (list (struct {
+          originalDir = drv;
+          stowPackage = string;
+        }))
+        drv
+       ] )
+    (dirs:
+      depot.nix.runExecline "make-stow-dir" {
+        stdin = lib.pipe dirs [
+          (map depot.users.Profpatsch.netencode.gen.dwim)
+          depot.users.Profpatsch.netstring.toNetstringList
+        ];
+      } [
+        "importas" "out" "out"
+        "if" [ bins.mkdir "-p" "$out" ]
+        "forstdin" "-d" "" "-o" "0" "line"
+        "pipeline" [
+          depot.users.Profpatsch.execline.print-one-env "line"
+        ]
+        depot.users.Profpatsch.netencode.record-splice-env
+        "importas" "-ui" "originalDir" "originalDir"
+        "importas" "-ui" "stowPackage" "stowPackage"
+        bins.ln "-sT" "$originalDir" "\${out}/\${stowPackage}"
+      ]);
+
+in
+
+# TODO: temp
+lib.pipe {} [
+  (_: makeStowDir [{
+    stowPackage = "hello";
+    originalDir = pkgs.hello;
+  }])
+  (d: runStow {
+    stowDir = d;
+    stowPackage = "hello";
+    targetDir = "/home/philip/tmp";
+    stowDirOriginPath = "/home/philip/tmp/stowOrigin";
+  })
+]