2024-02-27 14:56:15 +00:00
|
|
|
{
|
|
|
|
buildPackages,
|
|
|
|
gixy,
|
|
|
|
lib,
|
|
|
|
libiconv,
|
|
|
|
makeBinaryWrapper,
|
|
|
|
mkNugetDeps,
|
|
|
|
mkNugetSource,
|
|
|
|
pkgs,
|
|
|
|
stdenv,
|
|
|
|
}:
|
2023-07-22 11:48:25 +00:00
|
|
|
let
|
|
|
|
inherit (lib)
|
|
|
|
concatMapStringsSep
|
|
|
|
elem
|
|
|
|
escapeShellArg
|
|
|
|
last
|
|
|
|
optionalString
|
|
|
|
strings
|
|
|
|
types
|
|
|
|
;
|
|
|
|
in
|
|
|
|
rec {
|
2024-08-09 12:04:52 +00:00
|
|
|
/**
|
2024-08-12 15:42:11 +00:00
|
|
|
`makeScriptWriter` returns a derivation which creates an executable script.
|
2024-08-09 12:04:52 +00:00
|
|
|
|
|
|
|
# Inputs
|
|
|
|
|
|
|
|
config (AttrSet)
|
|
|
|
: `interpreter` (String)
|
2024-08-12 15:42:11 +00:00
|
|
|
: the [interpreter](https://en.wikipedia.org/wiki/Shebang_(Unix)) to use for the script.
|
2024-08-09 12:04:52 +00:00
|
|
|
: `check` (String)
|
2024-08-12 15:42:11 +00:00
|
|
|
: A command to check the script. For example, this could be a linting check.
|
2024-08-09 12:04:52 +00:00
|
|
|
: `makeWrapperArgs` (Optional, [ String ], Default: [])
|
2024-08-12 15:42:11 +00:00
|
|
|
: Arguments forwarded to (`makeWrapper`)[#fun-makeWrapper].
|
2024-08-09 12:04:52 +00:00
|
|
|
|
|
|
|
`nameOrPath` (String)
|
|
|
|
: The name of the script or the path to the script.
|
|
|
|
|
|
|
|
When a `string` starting with "/" is passed, the script will be created at the specified path in $out.
|
|
|
|
I.e. `"/bin/hello"` will create a script at `$out/bin/hello`.
|
|
|
|
|
2024-08-12 15:42:11 +00:00
|
|
|
Any other `string` is interpreted as a filename.
|
|
|
|
It must be a [POSIX filename](https://en.wikipedia.org/wiki/Filename) starting with a letter, digit, dot, or underscore.
|
2024-08-09 12:04:52 +00:00
|
|
|
Spaces or special characters are not allowed.
|
|
|
|
|
|
|
|
`content` (String)
|
|
|
|
: The content of the script.
|
|
|
|
|
|
|
|
:::{.note}
|
|
|
|
This function is used as base implementation for other high-level writer functions.
|
|
|
|
|
|
|
|
For example, `writeBash` can (roughly) be implemented as:
|
2024-08-12 15:42:11 +00:00
|
|
|
|
2024-08-09 12:04:52 +00:00
|
|
|
```nix
|
|
|
|
writeBash = makeScriptWriter { interpreter = "${pkgs.bash}/bin/bash"; }
|
|
|
|
```
|
|
|
|
:::
|
|
|
|
|
|
|
|
# Examples
|
|
|
|
:::{.example}
|
|
|
|
## `pkgs.writers.makeScriptWriter` dash example
|
|
|
|
|
|
|
|
```nix-repl
|
|
|
|
:b makeScriptWriter { interpreter = "${pkgs.dash}/bin/dash"; } "hello" "echo hello world"
|
|
|
|
-> /nix/store/indvlr9ckmnv4f0ynkmasv2h4fxhand0-hello
|
|
|
|
```
|
|
|
|
|
|
|
|
The above example creates a script named `hello` that outputs `hello world` when executed.
|
|
|
|
|
|
|
|
```sh
|
|
|
|
> /nix/store/indvlr9ckmnv4f0ynkmasv2h4fxhand0-hello
|
|
|
|
hello world
|
|
|
|
```
|
|
|
|
:::
|
|
|
|
|
|
|
|
:::{.example}
|
|
|
|
## `pkgs.writers.makeScriptWriter` python example
|
|
|
|
|
|
|
|
```nix-repl
|
|
|
|
:b makeScriptWriter { interpreter = "${pkgs.python3}/bin/python"; } "python-hello" "print('hello world')"
|
|
|
|
-> /nix/store/4kvby1hqr45ffcdrvfpnpj62hanskw93-python-hello
|
|
|
|
```
|
|
|
|
|
|
|
|
```sh
|
|
|
|
> /nix/store/4kvby1hqr45ffcdrvfpnpj62hanskw93-python-hello
|
|
|
|
hello world
|
|
|
|
```
|
|
|
|
:::
|
|
|
|
*/
|
|
|
|
makeScriptWriter =
|
|
|
|
{
|
|
|
|
interpreter,
|
|
|
|
check ? "",
|
|
|
|
makeWrapperArgs ? [ ],
|
|
|
|
}:
|
|
|
|
nameOrPath: content:
|
|
|
|
assert
|
|
|
|
(types.path.check nameOrPath)
|
|
|
|
|| (builtins.match "([0-9A-Za-z._])[0-9A-Za-z._-]*" nameOrPath != null);
|
2024-03-14 21:11:11 +00:00
|
|
|
assert (types.path.check content) || (types.str.check content);
|
2023-07-22 11:48:25 +00:00
|
|
|
let
|
2024-02-27 14:56:15 +00:00
|
|
|
nameIsPath = types.path.check nameOrPath;
|
2023-07-22 11:48:25 +00:00
|
|
|
name = last (builtins.split "/" nameOrPath);
|
2024-02-27 14:56:15 +00:00
|
|
|
path = if nameIsPath then nameOrPath else "/bin/${name}";
|
|
|
|
# The inner derivation which creates the executable under $out/bin (never at $out directly)
|
2024-08-12 15:42:11 +00:00
|
|
|
# This is required in order to support wrapping, as wrapped programs consist of
|
|
|
|
# at least two files: the executable and the wrapper.
|
2024-02-27 14:56:15 +00:00
|
|
|
inner =
|
2024-08-09 12:04:52 +00:00
|
|
|
pkgs.runCommandLocal name
|
|
|
|
(
|
|
|
|
{
|
|
|
|
inherit makeWrapperArgs;
|
|
|
|
nativeBuildInputs = [ makeBinaryWrapper ];
|
|
|
|
meta.mainProgram = name;
|
2024-02-27 14:56:15 +00:00
|
|
|
}
|
2024-08-09 12:04:52 +00:00
|
|
|
// (
|
|
|
|
if (types.str.check content) then
|
|
|
|
{
|
|
|
|
inherit content interpreter;
|
|
|
|
passAsFile = [ "content" ];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
inherit interpreter;
|
|
|
|
contentPath = content;
|
|
|
|
}
|
|
|
|
)
|
2024-02-27 14:56:15 +00:00
|
|
|
)
|
2024-08-09 12:04:52 +00: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
|
2024-02-27 14:56:15 +00:00
|
|
|
then
|
2024-08-09 12:04:52 +00:00
|
|
|
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
|
2024-02-27 14:56:15 +00:00
|
|
|
fi
|
|
|
|
|
2024-08-09 12:04:52 +00:00
|
|
|
echo "#! $interpreterLine" > $out
|
|
|
|
cat "$contentPath" >> $out
|
|
|
|
${optionalString (check != "") ''
|
|
|
|
${check} $out
|
|
|
|
''}
|
|
|
|
chmod +x $out
|
|
|
|
|
|
|
|
# Relocate executable
|
|
|
|
# Wrap it if makeWrapperArgs are specified
|
|
|
|
mv $out tmp
|
|
|
|
mkdir -p $out/$(dirname "${path}")
|
|
|
|
mv tmp $out/${path}
|
|
|
|
if [ -n "''${makeWrapperArgs+''${makeWrapperArgs[@]}}" ]; then
|
|
|
|
wrapProgram $out/${path} ''${makeWrapperArgs[@]}
|
|
|
|
fi
|
|
|
|
'';
|
|
|
|
in
|
|
|
|
if nameIsPath then
|
|
|
|
inner
|
|
|
|
# In case nameOrPath is a name, the user intends the executable to be located at $out.
|
|
|
|
# This is achieved by creating a separate derivation containing a symlink at $out linking to ${inner}/bin/${name}.
|
|
|
|
# This breaks the override pattern.
|
|
|
|
# In case this turns out to be a problem, we can still add more magic
|
|
|
|
else
|
|
|
|
pkgs.runCommandLocal name { } ''
|
|
|
|
ln -s ${inner}/bin/${name} $out
|
|
|
|
'';
|
|
|
|
|
|
|
|
/**
|
2024-08-12 15:42:11 +00:00
|
|
|
`makeBinWriter` returns a derivation which compiles the given script into an executable format.
|
2024-08-09 12:04:52 +00:00
|
|
|
|
|
|
|
:::{.note}
|
2024-08-12 15:42:11 +00:00
|
|
|
This function is the base implementation for other compile language `writers`, such as `writeHaskell` and `writeRust`.
|
2024-08-09 12:04:52 +00:00
|
|
|
:::
|
|
|
|
|
|
|
|
# Inputs
|
|
|
|
|
|
|
|
config (AttrSet)
|
|
|
|
: `compileScript` (String)
|
|
|
|
: The script that compiles the given content into an executable.
|
|
|
|
|
|
|
|
: `strip` (Boolean, Default: true)
|
2024-08-12 15:42:11 +00:00
|
|
|
: Whether to [strip](https://nixos.org/manual/nixpkgs/stable/#ssec-fixup-phase) the executable or not.
|
2024-08-09 12:04:52 +00:00
|
|
|
|
|
|
|
: `makeWrapperArgs` (Optional, [ String ], Default: [])
|
|
|
|
: Arguments forwarded to (`makeWrapper`)[#fun-makeWrapper]
|
|
|
|
|
|
|
|
`nameOrPath` (String)
|
|
|
|
: The name of the script or the path to the script.
|
|
|
|
|
|
|
|
When a `string` starting with "/" is passed, the script will be created at the specified path in $out.
|
2024-08-12 15:42:11 +00:00
|
|
|
For example, `"/bin/hello"` will create a script at `$out/bin/hello`.
|
2024-08-09 12:04:52 +00:00
|
|
|
|
2024-08-12 15:42:11 +00:00
|
|
|
Any other `string` is interpreted as a filename.
|
|
|
|
It must be a [POSIX filename](https://en.wikipedia.org/wiki/Filename) starting with a letter, digit, dot, or underscore.
|
2024-08-09 12:04:52 +00:00
|
|
|
Spaces or special characters are not allowed.
|
|
|
|
|
|
|
|
# Examples
|
|
|
|
:::{.example}
|
|
|
|
## `pkgs.writers.makeBinWriter` example
|
|
|
|
|
|
|
|
```c
|
|
|
|
// main.c
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
int main()
|
|
|
|
{
|
|
|
|
printf("Hello, World!\n");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
```nix-repl
|
|
|
|
:b makeBinWriter { compileScript = "${pkgs.gcc}/bin/gcc -o $out $contentPath"; } "hello" ./main.c
|
|
|
|
out -> /nix/store/f6crc8mwj3lvcxqclw7n09cm8nb6kxbh-hello
|
|
|
|
```
|
|
|
|
|
|
|
|
The above example creates an executable named `hello` that outputs `Hello, World!` when executed.
|
|
|
|
|
|
|
|
```sh
|
|
|
|
> /nix/store/f6crc8mwj3lvcxqclw7n09cm8nb6kxbh-hello
|
|
|
|
Hello, World!
|
|
|
|
```
|
|
|
|
:::
|
|
|
|
*/
|
|
|
|
makeBinWriter =
|
|
|
|
{
|
|
|
|
compileScript,
|
|
|
|
strip ? true,
|
|
|
|
makeWrapperArgs ? [ ],
|
|
|
|
}:
|
|
|
|
nameOrPath: content:
|
|
|
|
assert
|
|
|
|
(types.path.check nameOrPath)
|
|
|
|
|| (builtins.match "([0-9A-Za-z._])[0-9A-Za-z._-]*" nameOrPath != null);
|
2024-03-14 21:11:11 +00:00
|
|
|
assert (types.path.check content) || (types.str.check content);
|
2023-07-22 11:48:25 +00:00
|
|
|
let
|
2024-02-27 14:56:15 +00:00
|
|
|
nameIsPath = types.path.check nameOrPath;
|
2023-07-22 11:48:25 +00:00
|
|
|
name = last (builtins.split "/" nameOrPath);
|
2024-02-27 14:56:15 +00:00
|
|
|
path = if nameIsPath then nameOrPath else "/bin/${name}";
|
|
|
|
# The inner derivation which creates the executable under $out/bin (never at $out directly)
|
|
|
|
# This is required in order to support wrapping, as wrapped programs consist of at least two files: the executable and the wrapper.
|
|
|
|
inner =
|
2024-08-09 12:04:52 +00:00
|
|
|
pkgs.runCommandLocal name
|
|
|
|
(
|
|
|
|
{
|
|
|
|
inherit makeWrapperArgs;
|
|
|
|
nativeBuildInputs = [ makeBinaryWrapper ];
|
|
|
|
meta.mainProgram = name;
|
|
|
|
}
|
|
|
|
// (
|
|
|
|
if (types.str.check content) then
|
|
|
|
{
|
|
|
|
inherit content;
|
|
|
|
passAsFile = [ "content" ];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{ contentPath = content; }
|
|
|
|
)
|
2024-02-27 14:56:15 +00:00
|
|
|
)
|
2024-08-09 12:04:52 +00:00
|
|
|
''
|
|
|
|
${compileScript}
|
|
|
|
${lib.optionalString strip "${lib.getBin buildPackages.bintools-unwrapped}/bin/${buildPackages.bintools-unwrapped.targetPrefix}strip -S $out"}
|
|
|
|
# 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"}
|
|
|
|
mv $out tmp
|
|
|
|
mkdir -p $out/$(dirname "${path}")
|
|
|
|
mv tmp $out/${path}
|
|
|
|
if [ -n "''${makeWrapperArgs+''${makeWrapperArgs[@]}}" ]; then
|
|
|
|
wrapProgram $out/${path} ''${makeWrapperArgs[@]}
|
|
|
|
fi
|
|
|
|
'';
|
2023-07-22 11:48:25 +00:00
|
|
|
in
|
2024-08-09 12:04:52 +00:00
|
|
|
if nameIsPath then
|
|
|
|
inner
|
|
|
|
# In case nameOrPath is a name, the user intends the executable to be located at $out.
|
|
|
|
# This is achieved by creating a separate derivation containing a symlink at $out linking to ${inner}/bin/${name}.
|
|
|
|
# This breaks the override pattern.
|
|
|
|
# In case this turns out to be a problem, we can still add more magic
|
|
|
|
else
|
|
|
|
pkgs.runCommandLocal name { } ''
|
2024-02-27 14:56:15 +00:00
|
|
|
ln -s ${inner}/bin/${name} $out
|
|
|
|
'';
|
2023-07-22 11:48:25 +00:00
|
|
|
|
|
|
|
# Like writeScript but the first line is a shebang to bash
|
|
|
|
#
|
2024-02-27 14:56:15 +00:00
|
|
|
# Can be called with or without extra arguments.
|
|
|
|
#
|
|
|
|
# Example without arguments:
|
2023-07-22 11:48:25 +00:00
|
|
|
# writeBash "example" ''
|
|
|
|
# echo hello world
|
|
|
|
# ''
|
2024-02-27 14:56:15 +00:00
|
|
|
#
|
|
|
|
# Example with arguments:
|
|
|
|
# writeBash "example"
|
|
|
|
# {
|
|
|
|
# makeWrapperArgs = [
|
|
|
|
# "--prefix" "PATH" ":" "${pkgs.hello}/bin"
|
|
|
|
# ];
|
|
|
|
# }
|
|
|
|
# ''
|
|
|
|
# hello
|
|
|
|
# ''
|
2024-08-09 12:04:52 +00:00
|
|
|
writeBash =
|
|
|
|
name: argsOrScript:
|
|
|
|
if lib.isAttrs argsOrScript && !lib.isDerivation argsOrScript then
|
|
|
|
makeScriptWriter (argsOrScript // { interpreter = "${lib.getExe pkgs.bash}"; }) name
|
|
|
|
else
|
|
|
|
makeScriptWriter { interpreter = "${lib.getExe pkgs.bash}"; } name argsOrScript;
|
2023-07-22 11:48:25 +00:00
|
|
|
|
|
|
|
# Like writeScriptBin but the first line is a shebang to bash
|
2024-02-27 14:56:15 +00:00
|
|
|
#
|
|
|
|
# Can be called with or without extra arguments.
|
|
|
|
#
|
|
|
|
# Example without arguments:
|
|
|
|
# writeBashBin "example" ''
|
|
|
|
# echo hello world
|
|
|
|
# ''
|
|
|
|
#
|
|
|
|
# Example with arguments:
|
|
|
|
# writeBashBin "example"
|
|
|
|
# {
|
|
|
|
# makeWrapperArgs = [
|
|
|
|
# "--prefix", "PATH", ":", "${pkgs.hello}/bin",
|
|
|
|
# ];
|
|
|
|
# }
|
|
|
|
# ''
|
|
|
|
# hello
|
|
|
|
# ''
|
2024-08-09 12:04:52 +00:00
|
|
|
writeBashBin = name: writeBash "/bin/${name}";
|
2023-07-22 11:48:25 +00:00
|
|
|
|
|
|
|
# Like writeScript but the first line is a shebang to dash
|
|
|
|
#
|
2024-02-27 14:56:15 +00:00
|
|
|
# Can be called with or without extra arguments.
|
|
|
|
#
|
|
|
|
# Example without arguments:
|
2023-07-22 11:48:25 +00:00
|
|
|
# writeDash "example" ''
|
|
|
|
# echo hello world
|
|
|
|
# ''
|
2024-02-27 14:56:15 +00:00
|
|
|
#
|
|
|
|
# Example with arguments:
|
|
|
|
# writeDash "example"
|
|
|
|
# {
|
|
|
|
# makeWrapperArgs = [
|
|
|
|
# "--prefix", "PATH", ":", "${pkgs.hello}/bin",
|
|
|
|
# ];
|
|
|
|
# }
|
|
|
|
# ''
|
|
|
|
# hello
|
|
|
|
# ''
|
2024-08-09 12:04:52 +00:00
|
|
|
writeDash =
|
|
|
|
name: argsOrScript:
|
|
|
|
if lib.isAttrs argsOrScript && !lib.isDerivation argsOrScript then
|
|
|
|
makeScriptWriter (argsOrScript // { interpreter = "${lib.getExe pkgs.dash}"; }) name
|
|
|
|
else
|
|
|
|
makeScriptWriter { interpreter = "${lib.getExe pkgs.dash}"; } name argsOrScript;
|
2023-07-22 11:48:25 +00:00
|
|
|
|
|
|
|
# Like writeScriptBin but the first line is a shebang to dash
|
2024-02-27 14:56:15 +00:00
|
|
|
#
|
|
|
|
# Can be called with or without extra arguments.
|
|
|
|
#
|
|
|
|
# Example without arguments:
|
|
|
|
# writeDashBin "example" ''
|
|
|
|
# echo hello world
|
|
|
|
# ''
|
|
|
|
#
|
|
|
|
# Example with arguments:
|
|
|
|
# writeDashBin "example"
|
|
|
|
# {
|
|
|
|
# makeWrapperArgs = [
|
|
|
|
# "--prefix", "PATH", ":", "${pkgs.hello}/bin",
|
|
|
|
# ];
|
|
|
|
# }
|
|
|
|
# ''
|
|
|
|
# hello
|
|
|
|
# ''
|
2024-08-09 12:04:52 +00:00
|
|
|
writeDashBin = name: writeDash "/bin/${name}";
|
2023-07-22 11:48:25 +00:00
|
|
|
|
|
|
|
# Like writeScript but the first line is a shebang to fish
|
|
|
|
#
|
2024-02-27 14:56:15 +00:00
|
|
|
# Can be called with or without extra arguments.
|
|
|
|
#
|
|
|
|
# Example without arguments:
|
2023-07-22 11:48:25 +00:00
|
|
|
# writeFish "example" ''
|
|
|
|
# echo hello world
|
|
|
|
# ''
|
2024-02-27 14:56:15 +00:00
|
|
|
#
|
|
|
|
# Example with arguments:
|
|
|
|
# writeFish "example"
|
|
|
|
# {
|
|
|
|
# makeWrapperArgs = [
|
|
|
|
# "--prefix", "PATH", ":", "${pkgs.hello}/bin",
|
|
|
|
# ];
|
|
|
|
# }
|
|
|
|
# ''
|
|
|
|
# hello
|
|
|
|
# ''
|
2024-08-09 12:04:52 +00:00
|
|
|
writeFish =
|
|
|
|
name: argsOrScript:
|
|
|
|
if lib.isAttrs argsOrScript && !lib.isDerivation argsOrScript then
|
|
|
|
makeScriptWriter (
|
|
|
|
argsOrScript
|
|
|
|
// {
|
|
|
|
interpreter = "${lib.getExe pkgs.fish} --no-config";
|
|
|
|
check = "${lib.getExe pkgs.fish} --no-config --no-execute"; # syntax check only
|
|
|
|
}
|
|
|
|
) name
|
|
|
|
else
|
|
|
|
makeScriptWriter {
|
|
|
|
interpreter = "${lib.getExe pkgs.fish} --no-config";
|
|
|
|
check = "${lib.getExe pkgs.fish} --no-config --no-execute"; # syntax check only
|
|
|
|
} name argsOrScript;
|
2023-07-22 11:48:25 +00:00
|
|
|
|
|
|
|
# Like writeScriptBin but the first line is a shebang to fish
|
2024-02-27 14:56:15 +00:00
|
|
|
#
|
|
|
|
# Can be called with or without extra arguments.
|
|
|
|
#
|
|
|
|
# Example without arguments:
|
|
|
|
# writeFishBin "example" ''
|
|
|
|
# echo hello world
|
|
|
|
# ''
|
|
|
|
#
|
|
|
|
# Example with arguments:
|
|
|
|
# writeFishBin "example"
|
|
|
|
# {
|
|
|
|
# makeWrapperArgs = [
|
|
|
|
# "--prefix", "PATH", ":", "${pkgs.hello}/bin",
|
|
|
|
# ];
|
|
|
|
# }
|
|
|
|
# ''
|
|
|
|
# hello
|
|
|
|
# ''
|
2024-08-09 12:04:52 +00:00
|
|
|
writeFishBin = name: writeFish "/bin/${name}";
|
2023-07-22 11:48:25 +00:00
|
|
|
|
|
|
|
# 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 ]; } ''
|
|
|
|
# import Acme.Missiles
|
|
|
|
#
|
|
|
|
# main = launchMissiles
|
|
|
|
# '';
|
2024-08-09 12:04:52 +00:00
|
|
|
writeHaskell =
|
|
|
|
name:
|
|
|
|
{
|
|
|
|
ghc ? pkgs.ghc,
|
|
|
|
ghcArgs ? [ ],
|
|
|
|
libraries ? [ ],
|
|
|
|
makeWrapperArgs ? [ ],
|
|
|
|
strip ? true,
|
|
|
|
threadedRuntime ? true,
|
|
|
|
}:
|
2023-07-22 11:48:25 +00:00
|
|
|
let
|
|
|
|
appendIfNotSet = el: list: if elem el list then list else list ++ [ el ];
|
|
|
|
ghcArgs' = if threadedRuntime then appendIfNotSet "-threaded" ghcArgs else ghcArgs;
|
|
|
|
|
2024-08-09 12:04:52 +00:00
|
|
|
in
|
|
|
|
makeBinWriter {
|
2023-07-22 11:48:25 +00:00
|
|
|
compileScript = ''
|
|
|
|
cp $contentPath tmp.hs
|
2024-08-09 12:04:52 +00:00
|
|
|
${(ghc.withPackages (_: libraries))}/bin/ghc ${lib.escapeShellArgs ghcArgs'} tmp.hs
|
2023-07-22 11:48:25 +00:00
|
|
|
mv tmp $out
|
|
|
|
'';
|
2024-02-27 14:56:15 +00:00
|
|
|
inherit makeWrapperArgs strip;
|
2023-07-22 11:48:25 +00:00
|
|
|
} name;
|
|
|
|
|
2024-01-17 23:34:21 +00:00
|
|
|
# writeHaskellBin takes the same arguments as writeHaskell but outputs a directory (like writeScriptBin)
|
2024-08-09 12:04:52 +00:00
|
|
|
writeHaskellBin = name: writeHaskell "/bin/${name}";
|
2023-07-22 11:48:25 +00:00
|
|
|
|
2024-01-14 23:17:17 +00:00
|
|
|
# Like writeScript but the first line is a shebang to nu
|
|
|
|
#
|
2024-02-27 14:56:15 +00:00
|
|
|
# Can be called with or without extra arguments.
|
|
|
|
#
|
|
|
|
# Example without arguments:
|
2024-01-14 23:17:17 +00:00
|
|
|
# writeNu "example" ''
|
|
|
|
# echo hello world
|
|
|
|
# ''
|
2024-02-27 14:56:15 +00:00
|
|
|
#
|
|
|
|
# Example with arguments:
|
|
|
|
# writeNu "example"
|
|
|
|
# {
|
|
|
|
# makeWrapperArgs = [
|
|
|
|
# "--prefix", "PATH", ":", "${pkgs.hello}/bin",
|
|
|
|
# ];
|
|
|
|
# }
|
|
|
|
# ''
|
|
|
|
# hello
|
|
|
|
# ''
|
2024-08-09 12:04:52 +00:00
|
|
|
writeNu =
|
|
|
|
name: argsOrScript:
|
|
|
|
if lib.isAttrs argsOrScript && !lib.isDerivation argsOrScript then
|
|
|
|
makeScriptWriter (
|
|
|
|
argsOrScript // { interpreter = "${lib.getExe pkgs.nushell} --no-config-file"; }
|
|
|
|
) name
|
|
|
|
else
|
|
|
|
makeScriptWriter { interpreter = "${lib.getExe pkgs.nushell} --no-config-file"; } name argsOrScript;
|
2024-01-14 23:17:17 +00:00
|
|
|
|
|
|
|
# Like writeScriptBin but the first line is a shebang to nu
|
2024-02-27 14:56:15 +00:00
|
|
|
#
|
|
|
|
# Can be called with or without extra arguments.
|
|
|
|
#
|
|
|
|
# Example without arguments:
|
|
|
|
# writeNuBin "example" ''
|
|
|
|
# echo hello world
|
|
|
|
# ''
|
|
|
|
#
|
|
|
|
# Example with arguments:
|
|
|
|
# writeNuBin "example"
|
|
|
|
# {
|
|
|
|
# makeWrapperArgs = [
|
|
|
|
# "--prefix", "PATH", ":", "${pkgs.hello}/bin",
|
|
|
|
# ];
|
|
|
|
# }
|
|
|
|
# ''
|
|
|
|
# hello
|
|
|
|
# ''
|
2024-08-09 12:04:52 +00:00
|
|
|
writeNuBin = name: writeNu "/bin/${name}";
|
2024-01-14 23:17:17 +00:00
|
|
|
|
2024-01-16 04:51:17 +00:00
|
|
|
# makeRubyWriter takes ruby and compatible rubyPackages and produces ruby script writer,
|
|
|
|
# If any libraries are specified, ruby.withPackages is used as interpreter, otherwise the "bare" ruby is used.
|
2024-08-09 12:04:52 +00:00
|
|
|
makeRubyWriter =
|
|
|
|
ruby: rubyPackages: buildRubyPackages: name:
|
|
|
|
{
|
|
|
|
libraries ? [ ],
|
|
|
|
...
|
|
|
|
}@args:
|
|
|
|
makeScriptWriter (
|
|
|
|
(builtins.removeAttrs args [ "libraries" ])
|
|
|
|
// {
|
|
|
|
interpreter =
|
|
|
|
if libraries == [ ] then "${ruby}/bin/ruby" else "${(ruby.withPackages (ps: libraries))}/bin/ruby";
|
|
|
|
# Rubocop doesn't seem to like running in this fashion.
|
|
|
|
#check = (writeDash "rubocop.sh" ''
|
|
|
|
# exec ${lib.getExe buildRubyPackages.rubocop} "$1"
|
|
|
|
#'');
|
|
|
|
}
|
|
|
|
) name;
|
2024-01-16 04:51:17 +00:00
|
|
|
|
2024-01-14 23:17:17 +00:00
|
|
|
# Like writeScript but the first line is a shebang to ruby
|
|
|
|
#
|
|
|
|
# Example:
|
2024-02-27 14:56:15 +00:00
|
|
|
# writeRuby "example" { libraries = [ pkgs.rubyPackages.git ]; } ''
|
2024-01-14 23:17:17 +00:00
|
|
|
# puts "hello world"
|
|
|
|
# ''
|
2024-01-16 04:51:17 +00:00
|
|
|
writeRuby = makeRubyWriter pkgs.ruby pkgs.rubyPackages buildPackages.rubyPackages;
|
2024-01-14 23:17:17 +00:00
|
|
|
|
2024-08-09 12:04:52 +00:00
|
|
|
writeRubyBin = name: writeRuby "/bin/${name}";
|
2024-01-14 23:17:17 +00:00
|
|
|
|
2024-01-16 04:51:17 +00:00
|
|
|
# makeLuaWriter takes lua and compatible luaPackages and produces lua script writer,
|
|
|
|
# which validates the script with luacheck at build time. If any libraries are specified,
|
|
|
|
# lua.withPackages is used as interpreter, otherwise the "bare" lua is used.
|
2024-08-09 12:04:52 +00:00
|
|
|
makeLuaWriter =
|
|
|
|
lua: luaPackages: buildLuaPackages: name:
|
|
|
|
{
|
|
|
|
libraries ? [ ],
|
|
|
|
...
|
|
|
|
}@args:
|
|
|
|
makeScriptWriter (
|
|
|
|
(builtins.removeAttrs args [ "libraries" ])
|
|
|
|
// {
|
|
|
|
interpreter = lua.interpreter;
|
2024-02-27 14:56:15 +00:00
|
|
|
# if libraries == []
|
|
|
|
# then lua.interpreter
|
|
|
|
# else (lua.withPackages (ps: libraries)).interpreter
|
|
|
|
# This should support packages! I just cant figure out why some dependency collision happens whenever I try to run this.
|
2024-08-09 12:04:52 +00:00
|
|
|
check = (
|
|
|
|
writeDash "luacheck.sh" ''
|
|
|
|
exec ${buildLuaPackages.luacheck}/bin/luacheck "$1"
|
|
|
|
''
|
|
|
|
);
|
|
|
|
}
|
|
|
|
) name;
|
2024-01-16 04:51:17 +00:00
|
|
|
|
|
|
|
# writeLua takes a name an attributeset with libraries and some lua source code and
|
|
|
|
# returns an executable (should also work with luajit)
|
2024-01-14 23:17:17 +00:00
|
|
|
#
|
|
|
|
# Example:
|
2024-01-16 04:51:17 +00:00
|
|
|
# writeLua "test_lua" { libraries = [ pkgs.luaPackages.say ]; } ''
|
|
|
|
# s = require("say")
|
|
|
|
# s:set_namespace("en")
|
|
|
|
#
|
|
|
|
# s:set('money', 'I have %s dollars')
|
|
|
|
# s:set('wow', 'So much money!')
|
|
|
|
#
|
|
|
|
# print(s('money', {1000})) -- I have 1000 dollars
|
|
|
|
#
|
|
|
|
# s:set_namespace("fr") -- switch to french!
|
|
|
|
# s:set('wow', "Tant d'argent!")
|
|
|
|
#
|
|
|
|
# print(s('wow')) -- Tant d'argent!
|
|
|
|
# s:set_namespace("en") -- switch back to english!
|
|
|
|
# print(s('wow')) -- So much money!
|
|
|
|
# ''
|
|
|
|
writeLua = makeLuaWriter pkgs.lua pkgs.luaPackages buildPackages.luaPackages;
|
2024-01-14 23:17:17 +00:00
|
|
|
|
2024-08-09 12:04:52 +00:00
|
|
|
writeLuaBin = name: writeLua "/bin/${name}";
|
|
|
|
|
|
|
|
writeRust =
|
|
|
|
name:
|
|
|
|
{
|
|
|
|
makeWrapperArgs ? [ ],
|
|
|
|
rustc ? pkgs.rustc,
|
|
|
|
rustcArgs ? [ ],
|
|
|
|
strip ? true,
|
|
|
|
}:
|
|
|
|
let
|
|
|
|
darwinArgs = lib.optionals stdenv.isDarwin [ "-L${lib.getLib libiconv}/lib" ];
|
|
|
|
in
|
2023-07-22 11:48:25 +00:00
|
|
|
makeBinWriter {
|
|
|
|
compileScript = ''
|
|
|
|
cp "$contentPath" tmp.rs
|
2024-08-09 12:04:52 +00:00
|
|
|
PATH=${lib.makeBinPath [ pkgs.gcc ]} ${rustc}/bin/rustc ${lib.escapeShellArgs rustcArgs} ${lib.escapeShellArgs darwinArgs} -o "$out" tmp.rs
|
2023-07-22 11:48:25 +00:00
|
|
|
'';
|
2024-02-27 14:56:15 +00:00
|
|
|
inherit makeWrapperArgs strip;
|
2023-07-22 11:48:25 +00:00
|
|
|
} name;
|
|
|
|
|
2024-08-09 12:04:52 +00:00
|
|
|
writeRustBin = name: writeRust "/bin/${name}";
|
2023-07-22 11:48:25 +00:00
|
|
|
|
|
|
|
# writeJS takes a name an attributeset with libraries and some JavaScript sourcecode and
|
|
|
|
# returns an executable
|
|
|
|
#
|
|
|
|
# Example:
|
2024-07-03 19:23:00 +00:00
|
|
|
# writeJS "example" { libraries = [ pkgs.uglify-js ]; } ''
|
2023-07-22 11:48:25 +00:00
|
|
|
# var UglifyJS = require("uglify-js");
|
|
|
|
# var code = "function add(first, second) { return first + second; }";
|
|
|
|
# var result = UglifyJS.minify(code);
|
|
|
|
# console.log(result.code);
|
|
|
|
# ''
|
2024-08-09 12:04:52 +00:00
|
|
|
writeJS =
|
|
|
|
name:
|
|
|
|
{
|
|
|
|
libraries ? [ ],
|
|
|
|
}:
|
|
|
|
content:
|
|
|
|
let
|
|
|
|
node-env = pkgs.buildEnv {
|
|
|
|
name = "node";
|
|
|
|
paths = libraries;
|
|
|
|
pathsToLink = [ "/lib/node_modules" ];
|
|
|
|
};
|
|
|
|
in
|
|
|
|
writeDash name ''
|
|
|
|
export NODE_PATH=${node-env}/lib/node_modules
|
|
|
|
exec ${lib.getExe pkgs.nodejs} ${pkgs.writeText "js" content} "$@"
|
|
|
|
'';
|
2023-07-22 11:48:25 +00:00
|
|
|
|
|
|
|
# writeJSBin takes the same arguments as writeJS but outputs a directory (like writeScriptBin)
|
2024-08-09 12:04:52 +00:00
|
|
|
writeJSBin = name: writeJS "/bin/${name}";
|
2023-07-22 11:48:25 +00: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}
|
|
|
|
'';
|
|
|
|
|
2024-08-09 12:04:52 +00:00
|
|
|
writeNginxConfig =
|
|
|
|
name: text:
|
|
|
|
pkgs.runCommandLocal name
|
|
|
|
{
|
|
|
|
inherit text;
|
|
|
|
passAsFile = [ "text" ];
|
|
|
|
nativeBuildInputs = [ gixy ];
|
|
|
|
} # sh
|
|
|
|
''
|
|
|
|
# nginx-config-formatter has an error - https://github.com/1connect/nginx-config-formatter/issues/16
|
|
|
|
awk -f ${awkFormatNginx} "$textPath" | sed '/^\s*$/d' > $out
|
|
|
|
gixy $out
|
|
|
|
'';
|
|
|
|
|
2023-07-22 11:48:25 +00: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;
|
|
|
|
# ''
|
2024-08-09 12:04:52 +00:00
|
|
|
writePerl =
|
|
|
|
name:
|
|
|
|
{
|
|
|
|
libraries ? [ ],
|
|
|
|
...
|
|
|
|
}@args:
|
2024-02-27 14:56:15 +00:00
|
|
|
makeScriptWriter (
|
2024-08-09 12:04:52 +00:00
|
|
|
(builtins.removeAttrs args [ "libraries" ])
|
2024-02-27 14:56:15 +00:00
|
|
|
// {
|
|
|
|
interpreter = "${lib.getExe (pkgs.perl.withPackages (p: libraries))}";
|
|
|
|
}
|
|
|
|
) name;
|
2023-07-22 11:48:25 +00:00
|
|
|
|
|
|
|
# writePerlBin takes the same arguments as writePerl but outputs a directory (like writeScriptBin)
|
2024-08-09 12:04:52 +00:00
|
|
|
writePerlBin = name: writePerl "/bin/${name}";
|
2023-07-22 11:48:25 +00: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.
|
2024-08-09 12:04:52 +00:00
|
|
|
makePythonWriter =
|
|
|
|
python: pythonPackages: buildPythonPackages: name:
|
|
|
|
{
|
|
|
|
libraries ? [ ],
|
|
|
|
flakeIgnore ? [ ],
|
|
|
|
...
|
|
|
|
}@args:
|
|
|
|
let
|
|
|
|
ignoreAttribute =
|
|
|
|
optionalString (flakeIgnore != [ ])
|
|
|
|
"--ignore ${concatMapStringsSep "," escapeShellArg flakeIgnore}";
|
|
|
|
in
|
|
|
|
makeScriptWriter (
|
|
|
|
(builtins.removeAttrs args [
|
|
|
|
"libraries"
|
|
|
|
"flakeIgnore"
|
|
|
|
])
|
2024-02-27 14:56:15 +00:00
|
|
|
// {
|
|
|
|
interpreter =
|
|
|
|
if pythonPackages != pkgs.pypy2Packages || pythonPackages != pkgs.pypy3Packages then
|
2024-08-09 12:04:52 +00:00
|
|
|
if libraries == [ ] then python.interpreter else (python.withPackages (ps: libraries)).interpreter
|
|
|
|
else
|
|
|
|
python.interpreter;
|
|
|
|
check = optionalString python.isPy3k (
|
|
|
|
writeDash "pythoncheck.sh" ''
|
|
|
|
exec ${buildPythonPackages.flake8}/bin/flake8 --show-source ${ignoreAttribute} "$1"
|
|
|
|
''
|
|
|
|
);
|
2024-02-27 14:56:15 +00:00
|
|
|
}
|
2024-08-09 12:04:52 +00:00
|
|
|
) name;
|
2023-07-22 11:48:25 +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
|
|
|
|
# ''
|
|
|
|
writePyPy2 = makePythonWriter pkgs.pypy2 pkgs.pypy2Packages buildPackages.pypy2Packages;
|
|
|
|
|
|
|
|
# writePyPy2Bin takes the same arguments as writePyPy2 but outputs a directory (like writeScriptBin)
|
2024-08-09 12:04:52 +00:00
|
|
|
writePyPy2Bin = name: writePyPy2 "/bin/${name}";
|
2023-07-22 11:48:25 +00: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
|
|
|
|
#
|
2024-08-15 04:57:39 +00:00
|
|
|
# y = yaml.safe_load("""
|
2023-07-22 11:48:25 +00:00
|
|
|
# - test: success
|
|
|
|
# """)
|
|
|
|
# print(y[0]['test'])
|
|
|
|
# ''
|
|
|
|
writePython3 = makePythonWriter pkgs.python3 pkgs.python3Packages buildPackages.python3Packages;
|
|
|
|
|
|
|
|
# writePython3Bin takes the same arguments as writePython3 but outputs a directory (like writeScriptBin)
|
2024-08-09 12:04:52 +00:00
|
|
|
writePython3Bin = name: writePython3 "/bin/${name}";
|
2023-07-22 11:48:25 +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
|
|
|
|
#
|
2024-08-15 04:57:39 +00:00
|
|
|
# y = yaml.safe_load("""
|
2023-07-22 11:48:25 +00:00
|
|
|
# - test: success
|
|
|
|
# """)
|
|
|
|
# print(y[0]['test'])
|
|
|
|
# ''
|
|
|
|
writePyPy3 = makePythonWriter pkgs.pypy3 pkgs.pypy3Packages buildPackages.pypy3Packages;
|
|
|
|
|
|
|
|
# writePyPy3Bin takes the same arguments as writePyPy3 but outputs a directory (like writeScriptBin)
|
2024-08-09 12:04:52 +00:00
|
|
|
writePyPy3Bin = name: writePyPy3 "/bin/${name}";
|
|
|
|
|
|
|
|
makeFSharpWriter =
|
|
|
|
{
|
|
|
|
dotnet-sdk ? pkgs.dotnet-sdk,
|
|
|
|
fsi-flags ? "",
|
|
|
|
libraries ? _: [ ],
|
|
|
|
...
|
|
|
|
}@args:
|
|
|
|
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 = "Nuget source with the dependencies for ${fname}";
|
|
|
|
deps = [ _nugetDeps ];
|
|
|
|
};
|
|
|
|
|
|
|
|
fsi = writeBash "fsi" ''
|
|
|
|
export HOME=$NIX_BUILD_TOP/.home
|
|
|
|
export DOTNET_SKIP_FIRST_TIME_EXPERIENCE=1
|
|
|
|
export DOTNET_CLI_TELEMETRY_OPTOUT=1
|
|
|
|
export DOTNET_NOLOGO=1
|
|
|
|
script="$1"; shift
|
|
|
|
${lib.getExe dotnet-sdk} fsi --quiet --nologo --readline- ${fsi-flags} "$@" < "$script"
|
|
|
|
'';
|
2023-07-22 11:48:25 +00:00
|
|
|
|
2024-08-09 12:04:52 +00:00
|
|
|
in
|
|
|
|
content:
|
|
|
|
makeScriptWriter
|
|
|
|
(
|
|
|
|
(builtins.removeAttrs args [
|
|
|
|
"dotnet-sdk"
|
|
|
|
"fsi-flags"
|
|
|
|
"libraries"
|
|
|
|
])
|
|
|
|
// {
|
|
|
|
interpreter = fsi;
|
|
|
|
}
|
|
|
|
)
|
|
|
|
path
|
|
|
|
''
|
|
|
|
#i "nuget: ${nuget-source}/lib"
|
|
|
|
${content}
|
|
|
|
exit 0
|
|
|
|
'';
|
2023-07-22 11:48:25 +00:00
|
|
|
|
2024-08-09 12:04:52 +00:00
|
|
|
writeFSharp = makeFSharpWriter { };
|
2023-07-22 11:48:25 +00:00
|
|
|
|
2024-08-09 12:04:52 +00:00
|
|
|
writeFSharpBin = name: writeFSharp "/bin/${name}";
|
2023-07-22 11:48:25 +00:00
|
|
|
}
|