Skip to content

Commit 64046f0

Browse files
authored
writers: add writeNim and writeNimBin (#338857)
2 parents 5f821de + ca23669 commit 64046f0

File tree

2 files changed

+74
-0
lines changed

2 files changed

+74
-0
lines changed

pkgs/build-support/writers/scripts.nix

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -651,6 +651,53 @@ rec {
651651
*/
652652
writeHaskellBin = name: writeHaskell "/bin/${name}";
653653

654+
/**
655+
writeNim takes a name, an attrset with an optional Nim compiler, and some
656+
Nim source code, returning an executable.
657+
658+
# Examples
659+
:::{.example}
660+
## `pkgs.writers.writeNim` usage example
661+
662+
```nix
663+
writeNim "hello-nim" { nim = pkgs.nim2; } ''
664+
echo "hello nim"
665+
'';
666+
```
667+
:::
668+
*/
669+
writeNim =
670+
name:
671+
{
672+
makeWrapperArgs ? [ ],
673+
nim ? pkgs.nim2,
674+
nimCompileOptions ? { },
675+
strip ? true,
676+
}:
677+
let
678+
nimCompileCmdArgs = lib.cli.toGNUCommandLineShell { optionValueSeparator = ":"; } (
679+
{
680+
d = "release";
681+
nimcache = ".";
682+
}
683+
// nimCompileOptions
684+
);
685+
in
686+
makeBinWriter {
687+
compileScript = ''
688+
cp $contentPath tmp.nim
689+
${lib.getExe nim} compile ${nimCompileCmdArgs} tmp.nim
690+
mv tmp $out
691+
'';
692+
inherit makeWrapperArgs strip;
693+
} name;
694+
695+
/**
696+
writeNimBin takes the same arguments as writeNim but outputs a directory
697+
(like writeScriptBin)
698+
*/
699+
writeNimBin = name: writeNim "/bin/${name}";
700+
654701
/**
655702
Like writeScript but the first line is a shebang to nu
656703

pkgs/build-support/writers/test.nix

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ let
3131
writeJSBin
3232
writeJSON
3333
writeLua
34+
writeNim
35+
writeNimBin
3436
writeNu
3537
writePerl
3638
writePerlBin
@@ -109,6 +111,10 @@ recurseIntoAttrs {
109111
_ -> print "fail"
110112
'');
111113

114+
nim = expectSuccessBin (writeNimBin "test-writers-nim-bin" { } ''
115+
echo "success"
116+
'');
117+
112118
js = expectSuccessBin (writeJSBin "test-writers-js-bin" { libraries = [ nodePackages.semver ]; } ''
113119
var semver = require('semver');
114120
@@ -191,6 +197,10 @@ recurseIntoAttrs {
191197
end
192198
'');
193199

200+
nim = expectSuccess (writeNim "test-writers-nim" { } ''
201+
echo "success"
202+
'');
203+
194204
nu = expectSuccess (writeNu "test-writers-nushell" ''
195205
echo "success"
196206
'');
@@ -411,6 +421,23 @@ recurseIntoAttrs {
411421
''
412422
);
413423

424+
nim = expectSuccess (
425+
writeNim "test-writers-wrapping-nim"
426+
{
427+
makeWrapperArgs = [
428+
"--set"
429+
"ThaigerSprint"
430+
"Thailand"
431+
];
432+
}
433+
''
434+
import os
435+
436+
if getEnv("ThaigerSprint") == "Thailand":
437+
echo "success"
438+
''
439+
);
440+
414441
python = expectSuccess (
415442
writePython3 "test-writers-wrapping-python"
416443
{

0 commit comments

Comments
 (0)