diff --git a/pkgs/build-support/writers/scripts.nix b/pkgs/build-support/writers/scripts.nix index f0b9d34aa950..cd9fcf4918eb 100644 --- a/pkgs/build-support/writers/scripts.nix +++ b/pkgs/build-support/writers/scripts.nix @@ -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 diff --git a/pkgs/build-support/writers/test.nix b/pkgs/build-support/writers/test.nix index b7e8452947f0..c363d00605fd 100644 --- a/pkgs/build-support/writers/test.nix +++ b/pkgs/build-support/writers/test.nix @@ -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" {