writers: add writeNim and writeNimBin

This adds convenience writers for self-contained Nim programs.
Those are compiled into very small binaries.

Test with: `nix build .#pkgs.tests.writers.{bin,simple,wrapping}.nim`
This commit is contained in:
euxane 2024-09-01 19:05:16 +02:00
parent 1b42ea0abc
commit ca23669cf8
2 changed files with 74 additions and 0 deletions

View File

@ -651,6 +651,53 @@ rec {
*/
writeHaskellBin = name: writeHaskell "/bin/${name}";
/**
writeNim takes a name, an attrset with an optional Nim compiler, and some
Nim source code, returning an executable.
# Examples
:::{.example}
## `pkgs.writers.writeNim` usage example
```nix
writeNim "hello-nim" { nim = pkgs.nim2; } ''
echo "hello nim"
'';
```
:::
*/
writeNim =
name:
{
makeWrapperArgs ? [ ],
nim ? pkgs.nim2,
nimCompileOptions ? { },
strip ? true,
}:
let
nimCompileCmdArgs = lib.cli.toGNUCommandLineShell { optionValueSeparator = ":"; } (
{
d = "release";
nimcache = ".";
}
// nimCompileOptions
);
in
makeBinWriter {
compileScript = ''
cp $contentPath tmp.nim
${lib.getExe nim} compile ${nimCompileCmdArgs} tmp.nim
mv tmp $out
'';
inherit makeWrapperArgs strip;
} name;
/**
writeNimBin takes the same arguments as writeNim but outputs a directory
(like writeScriptBin)
*/
writeNimBin = name: writeNim "/bin/${name}";
/**
Like writeScript but the first line is a shebang to nu

View File

@ -31,6 +31,8 @@ let
writeJSBin
writeJSON
writeLua
writeNim
writeNimBin
writeNu
writePerl
writePerlBin
@ -109,6 +111,10 @@ recurseIntoAttrs {
_ -> print "fail"
'');
nim = expectSuccessBin (writeNimBin "test-writers-nim-bin" { } ''
echo "success"
'');
js = expectSuccessBin (writeJSBin "test-writers-js-bin" { libraries = [ nodePackages.semver ]; } ''
var semver = require('semver');
@ -191,6 +197,10 @@ recurseIntoAttrs {
end
'');
nim = expectSuccess (writeNim "test-writers-nim" { } ''
echo "success"
'');
nu = expectSuccess (writeNu "test-writers-nushell" ''
echo "success"
'');
@ -411,6 +421,23 @@ recurseIntoAttrs {
''
);
nim = expectSuccess (
writeNim "test-writers-wrapping-nim"
{
makeWrapperArgs = [
"--set"
"ThaigerSprint"
"Thailand"
];
}
''
import os
if getEnv("ThaigerSprint") == "Thailand":
echo "success"
''
);
python = expectSuccess (
writePython3 "test-writers-wrapping-python"
{