2016-02-14 15:43:44 +00:00
|
|
|
{ config, lib, pkgs, ... }:
|
|
|
|
with config.krebs.lib;
|
2016-02-13 15:45:26 +00:00
|
|
|
rec {
|
|
|
|
execve = name: { filename, argv ? null, envp ? {}, destination ? "" }: let
|
|
|
|
in writeC name { inherit destination; } ''
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
static char *const filename = ${toC filename};
|
|
|
|
|
|
|
|
${if argv == null
|
|
|
|
then /* Propagate arguments */ ''
|
|
|
|
#define MAIN_ARGS int argc, char **argv
|
|
|
|
''
|
|
|
|
else /* Provide fixed arguments */ ''
|
|
|
|
#define MAIN_ARGS void
|
|
|
|
static char *const argv[] = ${toC (argv ++ [null])};
|
|
|
|
''}
|
|
|
|
|
|
|
|
static char *const envp[] = ${toC (
|
|
|
|
mapAttrsToList (k: v: "${k}=${v}") envp ++ [null]
|
|
|
|
)};
|
|
|
|
|
|
|
|
int main (MAIN_ARGS) {
|
|
|
|
execve(filename, argv, envp);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
'';
|
2016-02-08 00:23:06 +00:00
|
|
|
|
|
|
|
execveBin = name: cfg: execve name (cfg // { destination = "/bin/${name}"; });
|
|
|
|
|
|
|
|
writeC = name: { destination ? "" }: src: pkgs.runCommand name {} ''
|
2016-03-03 18:45:46 +00:00
|
|
|
PATH=${makeBinPath (with pkgs; [
|
2016-02-08 00:23:06 +00:00
|
|
|
binutils
|
|
|
|
coreutils
|
|
|
|
gcc
|
|
|
|
])}
|
|
|
|
src=${pkgs.writeText "${name}.c" src}
|
|
|
|
exe=$out${destination}
|
|
|
|
mkdir -p "$(dirname "$exe")"
|
|
|
|
gcc -O -Wall -o "$exe" $src
|
|
|
|
strip --strip-unneeded "$exe"
|
|
|
|
'';
|
|
|
|
|
|
|
|
writeDash = name: text: pkgs.writeScript name ''
|
|
|
|
#! ${pkgs.dash}/bin/dash
|
|
|
|
${text}
|
|
|
|
'';
|
|
|
|
|
|
|
|
writeDashBin = name: text: pkgs.writeTextFile {
|
|
|
|
executable = true;
|
|
|
|
destination = "/bin/${name}";
|
|
|
|
name = name;
|
|
|
|
text = ''
|
|
|
|
#! ${pkgs.dash}/bin/dash
|
|
|
|
${text}
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
writeNixFromCabal = name: path: pkgs.runCommand name {} ''
|
|
|
|
${pkgs.cabal2nix}/bin/cabal2nix ${path} > $out
|
|
|
|
'';
|
|
|
|
}
|