From 498ba41a450a45d546204b35f7f7ca0f83a049d6 Mon Sep 17 00:00:00 2001
From: Nick Cao <nickcao@nichi.co>
Date: Mon, 1 Aug 2022 20:42:58 +0800
Subject: [PATCH] stratisd: init at 3.2.2

---
 pkgs/tools/filesystems/stratisd/default.nix | 105 ++++++++++++++++++++
 pkgs/tools/filesystems/stratisd/paths.patch |  42 ++++++++
 pkgs/top-level/all-packages.nix             |   2 +
 3 files changed, 149 insertions(+)
 create mode 100644 pkgs/tools/filesystems/stratisd/default.nix
 create mode 100644 pkgs/tools/filesystems/stratisd/paths.patch

diff --git a/pkgs/tools/filesystems/stratisd/default.nix b/pkgs/tools/filesystems/stratisd/default.nix
new file mode 100644
index 000000000000..1302c10ea8c1
--- /dev/null
+++ b/pkgs/tools/filesystems/stratisd/default.nix
@@ -0,0 +1,105 @@
+{ lib
+, stdenv
+, fetchFromGitHub
+, rustPlatform
+, pkg-config
+, asciidoc
+, dbus
+, cryptsetup
+, util-linux
+, udev
+, systemd
+, xfsprogs
+, thin-provisioning-tools
+, clevis
+, jose
+, jq
+, curl
+, tpm2-tools
+, coreutils
+, clevisSupport ? false
+}:
+
+stdenv.mkDerivation rec {
+  pname = "stratisd";
+  version = "3.2.2";
+
+  src = fetchFromGitHub {
+    owner = "stratis-storage";
+    repo = pname;
+    rev = "v${version}";
+    hash = "sha256-dNbbKGRLSYVnPdKfxlLIwXNEf7P6EvGbOp8sfpaw38g=";
+  };
+
+  cargoDeps = rustPlatform.fetchCargoTarball {
+    inherit src;
+    hash = "sha256-tJT0GKLpZtiQ/AZACkNeC3zgso54k/L03dFI0m1Jbls=";
+  };
+
+  patches = [
+    # Allow overriding BINARIES_PATHS with environment variable at compile time
+    ./paths.patch
+  ];
+
+  postPatch = ''
+    substituteInPlace udev/61-stratisd.rules \
+      --replace stratis-base32-decode "$out/lib/udev/stratis-base32-decode" \
+      --replace stratis-str-cmp       "$out/lib/udev/stratis-str-cmp"
+
+    substituteInPlace systemd/stratis-fstab-setup \
+      --replace stratis-min           "$out/bin/stratis-min" \
+      --replace systemd-ask-password  "${systemd}/bin/systemd-ask-password" \
+      --replace sleep                 "${coreutils}/bin/sleep" \
+      --replace udevadm               "${udev}/bin/udevadm"
+  '';
+
+  nativeBuildInputs = with rustPlatform; [
+    cargoSetupHook
+    bindgenHook
+    rust.cargo
+    rust.rustc
+    pkg-config
+    asciidoc
+  ];
+
+  buildInputs = [
+    dbus
+    cryptsetup
+    util-linux
+    udev
+  ];
+
+  BINARIES_PATHS = lib.makeBinPath ([
+    xfsprogs
+    thin-provisioning-tools
+    udev
+  ] ++ lib.optionals clevisSupport [
+    clevis
+    jose
+    jq
+    cryptsetup
+    curl
+    tpm2-tools
+    coreutils
+  ]);
+
+  makeFlags = [ "PREFIX=${placeholder "out"}" ];
+  buildFlags = [ "release" "release-min" "docs/stratisd.8" ];
+
+  doCheck = true;
+  checkTarget = "test";
+
+  # remove files for supporting dracut
+  postInstall = ''
+    rm -r "$out/lib/dracut"
+    rm -r "$out/lib/systemd/system-generators"
+  '';
+
+  meta = with lib; {
+    description = "Easy to use local storage management for Linux";
+    homepage = "https://stratis-storage.github.io";
+    license = licenses.mpl20;
+    maintainers = with maintainers; [ nickcao ];
+    platforms = [ "x86_64-linux" ];
+  };
+}
diff --git a/pkgs/tools/filesystems/stratisd/paths.patch b/pkgs/tools/filesystems/stratisd/paths.patch
new file mode 100644
index 000000000000..c63c9eb4a22c
--- /dev/null
+++ b/pkgs/tools/filesystems/stratisd/paths.patch
@@ -0,0 +1,42 @@
+diff --git a/src/engine/strat_engine/cmd.rs b/src/engine/strat_engine/cmd.rs
+index daaff70f..ed528f7f 100644
+--- a/src/engine/strat_engine/cmd.rs
++++ b/src/engine/strat_engine/cmd.rs
+@@ -39,8 +39,6 @@ use crate::{
+ // The maximum allowable size of the thinpool metadata device
+ const MAX_META_SIZE: MetaBlocks = MetaBlocks(255 * ((1 << 14) - 64));
+ 
+-const BINARIES_PATHS: [&str; 4] = ["/usr/sbin", "/sbin", "/usr/bin", "/bin"];
+-
+ /// Find the binary with the given name by looking in likely locations.
+ /// Return None if no binary was found.
+ /// Search an explicit list of directories rather than the user's PATH
+@@ -49,7 +47,7 @@ const BINARIES_PATHS: [&str; 4] = ["/usr/sbin", "/sbin", "/usr/bin", "/bin"];
+ fn find_binary(name: &str) -> Option<PathBuf> {
+     BINARIES_PATHS
+         .iter()
+-        .map(|pre| [pre, name].iter().collect::<PathBuf>())
++        .map(|pre| [pre, &name.into()].iter().collect::<PathBuf>())
+         .find(|path| path.exists())
+ }
+ 
+@@ -147,6 +145,10 @@ lazy_static! {
+         .and_then(|mut hm| hm
+             .remove(CLEVIS)
+             .and_then(|c| hm.remove(JOSE).map(|j| (c, j))));
++    static ref BINARIES_PATHS: Vec<PathBuf> = match std::option_env!("BINARIES_PATHS") {
++        Some(paths) => std::env::split_paths(paths).collect(),
++        None => ["/usr/sbin", "/sbin", "/usr/bin", "/bin"].iter().map(|p| p.into()).collect(),
++    };
+ }
+ 
+ /// Verify that all binaries that the engine might invoke are available at some
+@@ -160,7 +162,7 @@ pub fn verify_binaries() -> StratisResult<()> {
+             name,
+             BINARIES_PATHS
+                 .iter()
+-                .map(|p| format!("\"{}\"", p))
++                .map(|p| format!("\"{}\"", p.display()))
+                 .collect::<Vec<_>>()
+                 .join(", "),
+         ))),
diff --git a/pkgs/top-level/all-packages.nix b/pkgs/top-level/all-packages.nix
index 57b367e26f3b..3ff1a8d917f9 100644
--- a/pkgs/top-level/all-packages.nix
+++ b/pkgs/top-level/all-packages.nix
@@ -5847,6 +5847,8 @@ with pkgs;
 
   soupault = callPackage ../tools/typesetting/soupault { };
 
+  stratisd = callPackage ../tools/filesystems/stratisd { };
+
   strawberry = libsForQt5.callPackage ../applications/audio/strawberry { };
 
   schildichat-desktop = callPackage ../applications/networking/instant-messengers/schildichat/schildichat-desktop.nix {