extend nixpkgs's lib instead

This commit is contained in:
2025-07-11 20:34:45 -07:00
parent 3ba8c1a5a6
commit 7f7dc03a20
11 changed files with 90 additions and 82 deletions

66
lib.nix Normal file
View File

@@ -0,0 +1,66 @@
{
inputs,
pkgs,
...
}:
inputs.nixpkgs.lib.extend (
final: prev:
let
lib = prev;
in
{
ensureZfsMounts =
dirs:
pkgs.writeShellApplication {
name = "zfslistmounted";
runtimeInputs = with pkgs; [
zfs
gnugrep
gawk
coreutils
];
text = ''
#!/bin/sh
zfs list -o mountpoint,mounted -H | awk '$2 == "yes" {print $1}' | grep -c '${lib.strings.concatStringsSep "\|" dirs}' | grep -Fq ${toString (lib.length dirs)}
'';
};
serviceMountDeps = serviceName: dirs: {
systemd.services."${serviceName}_mounts" = {
unitConfig.Wants = "zfs.target";
serviceConfig = {
Type = "oneshot";
RemainAfterExit = true;
ExecStart = lib.getExe (final.ensureZfsMounts dirs);
};
};
systemd.services.${serviceName} = {
wants = [ "${serviceName}_mounts.service" ];
};
};
# stolen from: https://stackoverflow.com/a/42398526
optimizeWithFlags =
pkg: flags:
lib.overrideDerivation pkg (
old:
let
newflags = lib.foldl' (acc: x: "${acc} ${x}") "" flags;
oldflags = if (lib.hasAttr "NIX_CFLAGS_COMPILE" old) then "${old.NIX_CFLAGS_COMPILE}" else "";
in
{
NIX_CFLAGS_COMPILE = "${oldflags} ${newflags}";
# stdenv = pkgs.clang19Stdenv;
}
);
optimizePackage =
pkg:
final.optimizeWithFlags pkg [
"-O3"
"-march=znver3"
"-mtune=znver3"
];
}
)