Refactor lib.nix

This commit is contained in:
Alex David 2025-05-11 15:21:02 -07:00
commit 8ee01f1769

40
lib.nix
View file

@ -15,7 +15,7 @@
inherit pkgs lib helpers;
};
initial-state = lib.pipe {
initial-state = {
cmd = "${lib.getExe pkgs.bubblewrap}";
path = "${pkgs.coreutils}/bin";
argv = "\"$@\"";
@ -23,7 +23,10 @@
new-session = true;
hostname = "jail";
env = {};
} (with combinators; [
};
in lib.pipe initial-state (
# apply pre-user combinators
(with combinators; [
(unsafe-add-raw-args "--proc /proc")
(unsafe-add-raw-args "--dev /dev")
(unsafe-add-raw-args "--tmpfs /tmp")
@ -48,14 +51,13 @@
'')
(ro-bind (noescape (helpers.dataDirSubPath "passwd")) "/etc/passwd")
(ro-bind (noescape (helpers.dataDirSubPath "group")) "/etc/group")
]);
in lib.pipe combinators [
# collect opts
getOpts
(lib.foldl (acc: el: el acc) initial-state)
])
# finalize state
(with combinators; compose [
# apply user combinators
++ getOpts combinators
# apply post-user combinators
++ (with combinators; [
(s: set-env "PATH" s.path s)
(s: if s.new-session then unsafe-add-raw-args "--new-session" s else s)
(s: lib.foldr (envVar:
@ -65,13 +67,15 @@ in lib.pipe combinators [
])
# build jailed app from state
(state: ''
RUNTIME_ARGS=()
${state.runtime}
exec ${state.cmd} "''${RUNTIME_ARGS[@]}" -- ${exe-str} ${state.argv}
'')
(text: pkgs.writeShellApplication { inherit name text; })
++ [
(state: ''
RUNTIME_ARGS=()
${state.runtime}
exec ${state.cmd} "''${RUNTIME_ARGS[@]}" -- ${exe-str} ${state.argv}
'')
(text: pkgs.writeShellApplication { inherit name text; })
# forward man pages
(jailed: if exe ? man then jailed // { inherit (exe) man; } else jailed)
]
# forward man pages
(jailed: if exe ? man then jailed // { inherit (exe) man; } else jailed)
]
)