2022-03-31 19:11:26 +01:00
|
|
|
{ pkgs, config, buildPackages, lib, stdenv, libiconv, mkNugetDeps, mkNugetSource, gixy }:
|
2018-10-27 18:05:26 +01:00
|
|
|
|
2021-12-10 21:03:06 +00:00
|
|
|
let
|
2022-04-01 10:33:10 +01:00
|
|
|
aliases = if config.allowAliases then (import ./aliases.nix lib) else prev: {};
|
2021-12-10 21:03:06 +00:00
|
|
|
|
|
|
|
writers = with lib; rec {
|
2018-10-27 18:05:26 +01:00
|
|
|
# Base implementation for non-compiled executables.
|
|
|
|
# Takes an interpreter, for example `${pkgs.bash}/bin/bash`
|
|
|
|
#
|
|
|
|
# Examples:
|
|
|
|
# writeBash = makeScriptWriter { interpreter = "${pkgs.bash}/bin/bash"; }
|
|
|
|
# makeScriptWriter { interpreter = "${pkgs.dash}/bin/dash"; } "hello" "echo hello world"
|
2018-12-01 23:56:16 +00:00
|
|
|
makeScriptWriter = { interpreter, check ? "" }: nameOrPath: content:
|
|
|
|
assert lib.or (types.path.check nameOrPath) (builtins.match "([0-9A-Za-z._])[0-9A-Za-z._-]*" nameOrPath != null);
|
2019-08-08 21:48:27 +01:00
|
|
|
assert lib.or (types.path.check content) (types.str.check content);
|
2018-12-01 23:56:16 +00:00
|
|
|
let
|
|
|
|
name = last (builtins.split "/" nameOrPath);
|
|
|
|
in
|
2018-10-27 18:05:26 +01:00
|
|
|
|
2021-06-28 21:40:40 +01:00
|
|
|
pkgs.runCommandLocal name (if (types.str.check content) then {
|
2021-06-25 13:39:59 +01:00
|
|
|
inherit content interpreter;
|
|
|
|
passAsFile = [ "content" ];
|
2021-06-28 21:40:40 +01:00
|
|
|
} else {
|
|
|
|
inherit interpreter;
|
|
|
|
contentPath = content;
|
2018-12-01 23:56:16 +00:00
|
|
|
}) ''
|
2020-07-25 10:47:20 +01:00
|
|
|
# On darwin a script cannot be used as an interpreter in a shebang but
|
|
|
|
# there doesn't seem to be a limit to the size of shebang and multiple
|
|
|
|
# arguments to the interpreter are allowed.
|
|
|
|
if [[ -n "${toString pkgs.stdenvNoCC.isDarwin}" ]] && isScript $interpreter
|
|
|
|
then
|
|
|
|
wrapperInterpreterLine=$(head -1 "$interpreter" | tail -c+3)
|
|
|
|
# Get first word from the line (note: xargs echo remove leading spaces)
|
|
|
|
wrapperInterpreter=$(echo "$wrapperInterpreterLine" | xargs echo | cut -d " " -f1)
|
|
|
|
|
|
|
|
if isScript $wrapperInterpreter
|
|
|
|
then
|
|
|
|
echo "error: passed interpreter ($interpreter) is a script which has another script ($wrapperInterpreter) as an interpreter, which is not supported."
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
# This should work as long as wrapperInterpreter is a shell, which is
|
|
|
|
# the case for programs wrapped with makeWrapper, like
|
|
|
|
# python3.withPackages etc.
|
|
|
|
interpreterLine="$wrapperInterpreterLine $interpreter"
|
|
|
|
else
|
|
|
|
interpreterLine=$interpreter
|
|
|
|
fi
|
|
|
|
|
|
|
|
echo "#! $interpreterLine" > $out
|
2018-12-01 23:56:16 +00:00
|
|
|
cat "$contentPath" >> $out
|
2019-05-26 11:25:59 +01:00
|
|
|
${optionalString (check != "") ''
|
|
|
|
${check} $out
|
|
|
|
''}
|
2018-12-01 23:56:16 +00:00
|
|
|
chmod +x $out
|
|
|
|
${optionalString (types.path.check nameOrPath) ''
|
|
|
|
mv $out tmp
|
|
|
|
mkdir -p $out/$(dirname "${nameOrPath}")
|
|
|
|
mv tmp $out/${nameOrPath}
|
|
|
|
''}
|
|
|
|
'';
|
|
|
|
|
|
|
|
# Base implementation for compiled executables.
|
|
|
|
# Takes a compile script, which in turn takes the name as an argument.
|
|
|
|
#
|
|
|
|
# Examples:
|
|
|
|
# writeSimpleC = makeBinWriter { compileScript = name: "gcc -o $out $contentPath"; }
|
2021-01-11 21:13:51 +00:00
|
|
|
makeBinWriter = { compileScript, strip ? true }: nameOrPath: content:
|
2018-12-01 23:56:16 +00:00
|
|
|
assert lib.or (types.path.check nameOrPath) (builtins.match "([0-9A-Za-z._])[0-9A-Za-z._-]*" nameOrPath != null);
|
2019-08-08 21:48:27 +01:00
|
|
|
assert lib.or (types.path.check content) (types.str.check content);
|
2018-12-01 23:56:16 +00:00
|
|
|
let
|
|
|
|
name = last (builtins.split "/" nameOrPath);
|
|
|
|
in
|
2019-08-08 21:48:27 +01:00
|
|
|
pkgs.runCommand name (if (types.str.check content) then {
|
2018-12-01 23:56:16 +00:00
|
|
|
inherit content;
|
|
|
|
passAsFile = [ "content" ];
|
|
|
|
} else {
|
|
|
|
contentPath = content;
|
|
|
|
}) ''
|
|
|
|
${compileScript}
|
2021-01-11 21:14:33 +00:00
|
|
|
${lib.optionalString strip
|
2021-10-29 11:12:58 +01:00
|
|
|
"${lib.getBin buildPackages.bintools-unwrapped}/bin/${buildPackages.bintools-unwrapped.targetPrefix}strip -S $out"}
|
2021-10-29 11:19:17 +01:00
|
|
|
# Sometimes binaries produced for darwin (e. g. by GHC) won't be valid
|
|
|
|
# mach-o executables from the get-go, but need to be corrected somehow
|
|
|
|
# which is done by fixupPhase.
|
|
|
|
${lib.optionalString pkgs.stdenvNoCC.hostPlatform.isDarwin "fixupPhase"}
|
2018-12-01 23:56:16 +00:00
|
|
|
${optionalString (types.path.check nameOrPath) ''
|
|
|
|
mv $out tmp
|
|
|
|
mkdir -p $out/$(dirname "${nameOrPath}")
|
|
|
|
mv tmp $out/${nameOrPath}
|
|
|
|
''}
|
|
|
|
'';
|
2018-10-27 18:05:26 +01:00
|
|
|
|
|
|
|
# Like writeScript but the first line is a shebang to bash
|
|
|
|
#
|
|
|
|
# Example:
|
|
|
|
# writeBash "example" ''
|
|
|
|
# echo hello world
|
|
|
|
# ''
|
|
|
|
writeBash = makeScriptWriter {
|
|
|
|
interpreter = "${pkgs.bash}/bin/bash";
|
|
|
|
};
|
|
|
|
|
|
|
|
# Like writeScriptBIn but the first line is a shebang to bash
|
|
|
|
writeBashBin = name:
|
|
|
|
writeBash "/bin/${name}";
|
|
|
|
|
|
|
|
# Like writeScript but the first line is a shebang to dash
|
|
|
|
#
|
|
|
|
# Example:
|
|
|
|
# writeDash "example" ''
|
|
|
|
# echo hello world
|
|
|
|
# ''
|
|
|
|
writeDash = makeScriptWriter {
|
|
|
|
interpreter = "${pkgs.dash}/bin/dash";
|
|
|
|
};
|
|
|
|
|
|
|
|
# Like writeScriptBin but the first line is a shebang to dash
|
|
|
|
writeDashBin = name:
|
|
|
|
writeDash "/bin/${name}";
|
|
|
|
|
|
|
|
# writeHaskell takes a name, an attrset with libraries and haskell version (both optional)
|
|
|
|
# and some haskell source code and returns an executable.
|
|
|
|
#
|
|
|
|
# Example:
|
|
|
|
# writeHaskell "missiles" { libraries = [ pkgs.haskellPackages.acme-missiles ]; } ''
|
2018-12-01 23:56:16 +00:00
|
|
|
# import Acme.Missiles
|
2018-10-27 18:05:26 +01:00
|
|
|
#
|
|
|
|
# main = launchMissiles
|
|
|
|
# '';
|
|
|
|
writeHaskell = name: {
|
|
|
|
libraries ? [],
|
2020-09-07 18:36:58 +01:00
|
|
|
ghc ? pkgs.ghc,
|
2021-01-11 21:14:33 +00:00
|
|
|
ghcArgs ? [],
|
2022-06-29 17:30:10 +01:00
|
|
|
threadedRuntime ? true,
|
2021-01-11 21:14:33 +00:00
|
|
|
strip ? true
|
2018-12-01 23:56:16 +00:00
|
|
|
}:
|
2022-06-29 17:30:10 +01:00
|
|
|
let
|
|
|
|
appendIfNotSet = el: list: if elem el list then list else list ++ [ el ];
|
|
|
|
ghcArgs' = if threadedRuntime then appendIfNotSet "-threaded" ghcArgs else ghcArgs;
|
|
|
|
|
|
|
|
in makeBinWriter {
|
2018-12-01 23:56:16 +00:00
|
|
|
compileScript = ''
|
|
|
|
cp $contentPath tmp.hs
|
2022-06-29 17:30:10 +01:00
|
|
|
${ghc.withPackages (_: libraries )}/bin/ghc ${lib.escapeShellArgs ghcArgs'} tmp.hs
|
2018-12-01 23:56:16 +00:00
|
|
|
mv tmp $out
|
|
|
|
'';
|
2021-01-11 21:14:33 +00:00
|
|
|
inherit strip;
|
2018-12-01 23:56:16 +00:00
|
|
|
} name;
|
2018-10-27 18:05:26 +01:00
|
|
|
|
|
|
|
# writeHaskellBin takes the same arguments as writeHaskell but outputs a directory (like writeScriptBin)
|
2018-12-01 23:56:16 +00:00
|
|
|
writeHaskellBin = name:
|
|
|
|
writeHaskell "/bin/${name}";
|
2018-10-27 18:05:26 +01:00
|
|
|
|
2021-01-11 21:14:33 +00:00
|
|
|
writeRust = name: {
|
|
|
|
rustc ? pkgs.rustc,
|
|
|
|
rustcArgs ? [],
|
|
|
|
strip ? true
|
|
|
|
}:
|
2021-11-13 00:04:36 +00:00
|
|
|
let
|
|
|
|
darwinArgs = lib.optionals stdenv.isDarwin [ "-L${lib.getLib libiconv}/lib" ];
|
|
|
|
in
|
2021-01-11 21:14:33 +00:00
|
|
|
makeBinWriter {
|
|
|
|
compileScript = ''
|
|
|
|
cp "$contentPath" tmp.rs
|
2021-11-13 00:04:36 +00:00
|
|
|
PATH=${makeBinPath [pkgs.gcc]} ${lib.getBin rustc}/bin/rustc ${lib.escapeShellArgs rustcArgs} ${lib.escapeShellArgs darwinArgs} -o "$out" tmp.rs
|
2021-01-11 21:14:33 +00:00
|
|
|
'';
|
|
|
|
inherit strip;
|
|
|
|
} name;
|
|
|
|
|
|
|
|
writeRustBin = name:
|
|
|
|
writeRust "/bin/${name}";
|
|
|
|
|
2018-10-27 18:05:26 +01:00
|
|
|
# writeJS takes a name an attributeset with libraries and some JavaScript sourcecode and
|
|
|
|
# returns an executable
|
|
|
|
#
|
|
|
|
# Example:
|
|
|
|
# writeJS "example" { libraries = [ pkgs.nodePackages.uglify-js ]; } ''
|
|
|
|
# var UglifyJS = require("uglify-js");
|
|
|
|
# var code = "function add(first, second) { return first + second; }";
|
|
|
|
# var result = UglifyJS.minify(code);
|
|
|
|
# console.log(result.code);
|
|
|
|
# ''
|
2018-12-01 23:56:16 +00:00
|
|
|
writeJS = name: { libraries ? [] }: content:
|
2018-10-27 18:05:26 +01:00
|
|
|
let
|
|
|
|
node-env = pkgs.buildEnv {
|
|
|
|
name = "node";
|
|
|
|
paths = libraries;
|
|
|
|
pathsToLink = [
|
|
|
|
"/lib/node_modules"
|
|
|
|
];
|
|
|
|
};
|
|
|
|
in writeDash name ''
|
|
|
|
export NODE_PATH=${node-env}/lib/node_modules
|
2018-12-01 23:56:16 +00:00
|
|
|
exec ${pkgs.nodejs}/bin/node ${pkgs.writeText "js" content}
|
2018-10-27 18:05:26 +01:00
|
|
|
'';
|
|
|
|
|
|
|
|
# writeJSBin takes the same arguments as writeJS but outputs a directory (like writeScriptBin)
|
|
|
|
writeJSBin = name:
|
|
|
|
writeJS "/bin/${name}";
|
|
|
|
|
2019-04-08 14:44:23 +01:00
|
|
|
awkFormatNginx = builtins.toFile "awkFormat-nginx.awk" ''
|
|
|
|
awk -f
|
|
|
|
{sub(/^[ \t]+/,"");idx=0}
|
|
|
|
/\{/{ctx++;idx=1}
|
|
|
|
/\}/{ctx--}
|
|
|
|
{id="";for(i=idx;i<ctx;i++)id=sprintf("%s%s", id, "\t");printf "%s%s\n", id, $0}
|
|
|
|
'';
|
|
|
|
|
2020-02-18 18:53:18 +00:00
|
|
|
writeNginxConfig = name: text: pkgs.runCommandLocal name {
|
2019-03-20 17:46:43 +00:00
|
|
|
inherit text;
|
|
|
|
passAsFile = [ "text" ];
|
2022-03-31 19:11:26 +01:00
|
|
|
nativeBuildInputs = [ gixy ];
|
2019-03-20 17:46:43 +00:00
|
|
|
} /* sh */ ''
|
2019-04-08 14:44:23 +01:00
|
|
|
# nginx-config-formatter has an error - https://github.com/1connect/nginx-config-formatter/issues/16
|
2020-10-30 00:22:29 +00:00
|
|
|
awk -f ${awkFormatNginx} "$textPath" | sed '/^\s*$/d' > $out
|
|
|
|
gixy $out
|
2019-03-20 17:46:43 +00:00
|
|
|
'';
|
|
|
|
|
2018-10-27 18:05:26 +01:00
|
|
|
# writePerl takes a name an attributeset with libraries and some perl sourcecode and
|
|
|
|
# returns an executable
|
|
|
|
#
|
|
|
|
# Example:
|
|
|
|
# writePerl "example" { libraries = [ pkgs.perlPackages.boolean ]; } ''
|
|
|
|
# use boolean;
|
|
|
|
# print "Howdy!\n" if true;
|
|
|
|
# ''
|
|
|
|
writePerl = name: { libraries ? [] }:
|
2021-02-24 19:53:45 +00:00
|
|
|
makeScriptWriter {
|
|
|
|
interpreter = "${pkgs.perl.withPackages (p: libraries)}/bin/perl";
|
|
|
|
} name;
|
2018-10-27 18:05:26 +01:00
|
|
|
|
|
|
|
# writePerlBin takes the same arguments as writePerl but outputs a directory (like writeScriptBin)
|
|
|
|
writePerlBin = name:
|
|
|
|
writePerl "/bin/${name}";
|
|
|
|
|
2020-07-24 11:31:01 +01:00
|
|
|
# makePythonWriter takes python and compatible pythonPackages and produces python script writer,
|
|
|
|
# which validates the script with flake8 at build time. If any libraries are specified,
|
|
|
|
# python.withPackages is used as interpreter, otherwise the "bare" python is used.
|
2022-08-26 01:42:46 +01:00
|
|
|
makePythonWriter = python: pythonPackages: buildPythonPackages: name: { libraries ? [], flakeIgnore ? [] }:
|
2020-07-24 11:31:01 +01:00
|
|
|
let
|
|
|
|
ignoreAttribute = optionalString (flakeIgnore != []) "--ignore ${concatMapStringsSep "," escapeShellArg flakeIgnore}";
|
|
|
|
in
|
|
|
|
makeScriptWriter {
|
|
|
|
interpreter =
|
|
|
|
if libraries == []
|
|
|
|
then "${python}/bin/python"
|
|
|
|
else "${python.withPackages (ps: libraries)}/bin/python"
|
|
|
|
;
|
2021-12-03 09:53:12 +00:00
|
|
|
check = optionalString python.isPy3k (writeDash "pythoncheck.sh" ''
|
2022-08-26 01:42:46 +01:00
|
|
|
exec ${buildPythonPackages.flake8}/bin/flake8 --show-source ${ignoreAttribute} "$1"
|
2021-12-03 09:53:12 +00:00
|
|
|
'');
|
2020-07-24 11:31:01 +01:00
|
|
|
} name;
|
|
|
|
|
2021-12-11 13:02:38 +00:00
|
|
|
# writePyPy2 takes a name an attributeset with libraries and some pypy2 sourcecode and
|
|
|
|
# returns an executable
|
|
|
|
#
|
|
|
|
# Example:
|
|
|
|
# writePyPy2 "test_pypy2" { libraries = [ pkgs.pypy2Packages.enum ]; } ''
|
|
|
|
# from enum import Enum
|
|
|
|
#
|
|
|
|
# class Test(Enum):
|
|
|
|
# a = "success"
|
|
|
|
#
|
|
|
|
# print Test.a
|
|
|
|
# ''
|
2022-08-26 01:42:46 +01:00
|
|
|
writePyPy2 = makePythonWriter pkgs.pypy2 pkgs.pypy2Packages buildPackages.pypy2Packages;
|
2021-12-11 13:02:38 +00:00
|
|
|
|
|
|
|
# writePyPy2Bin takes the same arguments as writePyPy2 but outputs a directory (like writeScriptBin)
|
|
|
|
writePyPy2Bin = name:
|
|
|
|
writePyPy2 "/bin/${name}";
|
|
|
|
|
2018-10-27 18:05:26 +01:00
|
|
|
# writePython3 takes a name an attributeset with libraries and some python3 sourcecode and
|
|
|
|
# returns an executable
|
|
|
|
#
|
|
|
|
# Example:
|
|
|
|
# writePython3 "test_python3" { libraries = [ pkgs.python3Packages.pyyaml ]; } ''
|
|
|
|
# import yaml
|
|
|
|
#
|
|
|
|
# y = yaml.load("""
|
|
|
|
# - test: success
|
|
|
|
# """)
|
|
|
|
# print(y[0]['test'])
|
|
|
|
# ''
|
2022-08-26 01:42:46 +01:00
|
|
|
writePython3 = makePythonWriter pkgs.python3 pkgs.python3Packages buildPackages.python3Packages;
|
2018-10-27 18:05:26 +01:00
|
|
|
|
|
|
|
# writePython3Bin takes the same arguments as writePython3 but outputs a directory (like writeScriptBin)
|
|
|
|
writePython3Bin = name:
|
|
|
|
writePython3 "/bin/${name}";
|
2021-12-10 21:03:06 +00:00
|
|
|
|
2021-12-11 13:02:38 +00:00
|
|
|
# writePyPy3 takes a name an attributeset with libraries and some pypy3 sourcecode and
|
|
|
|
# returns an executable
|
|
|
|
#
|
|
|
|
# Example:
|
|
|
|
# writePyPy3 "test_pypy3" { libraries = [ pkgs.pypy3Packages.pyyaml ]; } ''
|
|
|
|
# import yaml
|
|
|
|
#
|
|
|
|
# y = yaml.load("""
|
|
|
|
# - test: success
|
|
|
|
# """)
|
|
|
|
# print(y[0]['test'])
|
|
|
|
# ''
|
2022-08-26 01:42:46 +01:00
|
|
|
writePyPy3 = makePythonWriter pkgs.pypy3 pkgs.pypy3Packages buildPackages.pypy3Packages;
|
2021-12-11 13:02:38 +00:00
|
|
|
|
|
|
|
# writePyPy3Bin takes the same arguments as writePyPy3 but outputs a directory (like writeScriptBin)
|
|
|
|
writePyPy3Bin = name:
|
|
|
|
writePyPy3 "/bin/${name}";
|
|
|
|
|
2022-02-28 22:40:43 +00:00
|
|
|
|
|
|
|
makeFSharpWriter = { dotnet-sdk ? pkgs.dotnet-sdk, fsi-flags ? "", libraries ? _: [] }: nameOrPath:
|
|
|
|
let
|
|
|
|
fname = last (builtins.split "/" nameOrPath);
|
|
|
|
path = if strings.hasSuffix ".fsx" nameOrPath then nameOrPath else "${nameOrPath}.fsx";
|
|
|
|
_nugetDeps = mkNugetDeps { name = "${fname}-nuget-deps"; nugetDeps = libraries; };
|
|
|
|
|
|
|
|
nuget-source = mkNugetSource {
|
|
|
|
name = "${fname}-nuget-source";
|
|
|
|
description = "A Nuget source with the dependencies for ${fname}";
|
|
|
|
deps = [ _nugetDeps ];
|
|
|
|
};
|
|
|
|
|
|
|
|
fsi = writeBash "fsi" ''
|
|
|
|
export DOTNET_SKIP_FIRST_TIME_EXPERIENCE=1
|
|
|
|
export DOTNET_CLI_TELEMETRY_OPTOUT=1
|
|
|
|
export DOTNET_NOLOGO=1
|
|
|
|
script="$1"; shift
|
|
|
|
${dotnet-sdk}/bin/dotnet fsi --quiet --nologo --readline- ${fsi-flags} "$@" < "$script"
|
|
|
|
'';
|
|
|
|
|
|
|
|
in content: writers.makeScriptWriter {
|
|
|
|
interpreter = fsi;
|
|
|
|
} path
|
|
|
|
''
|
|
|
|
#i "nuget: ${nuget-source}/lib"
|
|
|
|
${ content }
|
|
|
|
exit 0
|
|
|
|
'';
|
|
|
|
|
|
|
|
writeFSharp =
|
|
|
|
makeFSharpWriter {};
|
|
|
|
|
|
|
|
writeFSharpBin = name:
|
|
|
|
writeFSharp "/bin/${name}";
|
|
|
|
|
2021-12-10 21:03:06 +00:00
|
|
|
};
|
|
|
|
in
|
|
|
|
writers // (aliases writers)
|