tvl-depot/users/Profpatsch/execline/default.nix
Profpatsch 16461c4100 feat(users/Profpatsch/execline): add setsid
Small wrapper around `setsid(2)` that does not exist in the
execline-style tooling.

Is probably gonna fail iff our process is a session leader already,
but I haven’t tried that out yet.

Change-Id: I1a820f1c6d65ddc29c30995bfd56a760a9d6b341
Reviewed-on: https://cl.tvl.fyi/c/depot/+/11167
Tested-by: BuildkiteCI
Autosubmit: Profpatsch <mail@profpatsch.de>
Reviewed-by: Profpatsch <mail@profpatsch.de>
2024-03-16 22:36:55 +00:00

70 lines
1.8 KiB
Nix

{ depot, pkgs, lib, ... }:
let
exec-helpers-hs = pkgs.haskellPackages.mkDerivation {
pname = "exec-helpers";
version = "0.1.0";
src = depot.users.Profpatsch.exactSource ./. [
./exec-helpers.cabal
./ExecHelpers.hs
];
libraryHaskellDepends = [
depot.users.Profpatsch.my-prelude
];
isLibrary = true;
license = lib.licenses.mit;
};
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()
}
}
'';
setsid = depot.nix.writers.rustSimple
{
name = "setsid";
dependencies = [
depot.users.Profpatsch.execline.exec-helpers
depot.third_party.rust-crates.libc
];
} ''
use std::os::unix::ffi::OsStrExt;
use std::ffi::OsStr;
fn main() {
let (args, prog) = exec_helpers::args_for_exec("setsid", 1);
let envvar = OsStr::from_bytes(&args.get(0).expect("first argument must be envvar name to set"));
let sid: i32 = unsafe { libc::setsid() };
std::env::set_var(envvar, format!("{}", sid));
let env: Vec<(&[u8], &[u8])> = vec![];
exec_helpers::exec_into_args("getid", prog, env);
}
'';
in
depot.nix.readTree.drvTargets {
inherit
exec-helpers-hs
print-one-env
setsid
;
}