reformat script-writers with nixfmt

This commit is contained in:
Jörg Thalheim
2024-07-08 16:24:17 +02:00
parent f62c30f81d
commit 4f2f663b3b

View File

@@ -27,28 +27,36 @@ rec {
# Examples: # Examples:
# writeBash = makeScriptWriter { interpreter = "${pkgs.bash}/bin/bash"; } # writeBash = makeScriptWriter { interpreter = "${pkgs.bash}/bin/bash"; }
# makeScriptWriter { interpreter = "${pkgs.dash}/bin/dash"; } "hello" "echo hello world" # makeScriptWriter { interpreter = "${pkgs.dash}/bin/dash"; } "hello" "echo hello world"
makeScriptWriter = { interpreter, check ? "", makeWrapperArgs ? [], }: nameOrPath: content: makeScriptWriter =
assert lib.or (types.path.check nameOrPath) (builtins.match "([0-9A-Za-z._])[0-9A-Za-z._-]*" nameOrPath != null); {
interpreter,
check ? "",
makeWrapperArgs ? [ ],
}:
nameOrPath: content:
assert lib.or (types.path.check nameOrPath) (
builtins.match "([0-9A-Za-z._])[0-9A-Za-z._-]*" nameOrPath != null
);
assert lib.or (types.path.check content) (types.str.check content); assert lib.or (types.path.check content) (types.str.check content);
let let
name = last (builtins.split "/" nameOrPath); name = last (builtins.split "/" nameOrPath);
in in
pkgs.runCommandLocal name ( pkgs.runCommandLocal name
(
{ {
inherit makeWrapperArgs; inherit makeWrapperArgs;
nativeBuildInputs = [ nativeBuildInputs = [ makeWrapper ];
makeWrapper
];
}
// lib.optionalAttrs (nameOrPath == "/bin/${name}") {
meta.mainProgram = name;
} }
// lib.optionalAttrs (nameOrPath == "/bin/${name}") { meta.mainProgram = name; }
// ( // (
if (types.str.check content) then { if (types.str.check content) then
{
inherit content interpreter; inherit content interpreter;
passAsFile = [ "content" ]; passAsFile = [ "content" ];
} else { }
else
{
inherit interpreter; inherit interpreter;
contentPath = content; contentPath = content;
} }
@@ -101,23 +109,35 @@ rec {
# #
# Examples: # Examples:
# writeSimpleC = makeBinWriter { compileScript = name: "gcc -o $out $contentPath"; } # writeSimpleC = makeBinWriter { compileScript = name: "gcc -o $out $contentPath"; }
makeBinWriter = { compileScript, strip ? true }: nameOrPath: content: makeBinWriter =
assert lib.or (types.path.check nameOrPath) (builtins.match "([0-9A-Za-z._])[0-9A-Za-z._-]*" nameOrPath != null); {
compileScript,
strip ? true,
}:
nameOrPath: content:
assert lib.or (types.path.check nameOrPath) (
builtins.match "([0-9A-Za-z._])[0-9A-Za-z._-]*" nameOrPath != null
);
assert lib.or (types.path.check content) (types.str.check content); assert lib.or (types.path.check content) (types.str.check content);
let let
name = last (builtins.split "/" nameOrPath); name = last (builtins.split "/" nameOrPath);
in in
pkgs.runCommand name ((if (types.str.check content) then { pkgs.runCommand name
(
(
if (types.str.check content) then
{
inherit content; inherit content;
passAsFile = [ "content" ]; passAsFile = [ "content" ];
} else { }
contentPath = content; else
}) // lib.optionalAttrs (nameOrPath == "/bin/${name}") { { contentPath = content; }
meta.mainProgram = name; )
}) '' // lib.optionalAttrs (nameOrPath == "/bin/${name}") { meta.mainProgram = name; }
)
''
${compileScript} ${compileScript}
${lib.optionalString strip ${lib.optionalString strip "${lib.getBin buildPackages.bintools-unwrapped}/bin/${buildPackages.bintools-unwrapped.targetPrefix}strip -S $out"}
"${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 # 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 # mach-o executables from the get-go, but need to be corrected somehow
# which is done by fixupPhase. # which is done by fixupPhase.
@@ -135,13 +155,10 @@ rec {
# writeBash "example" '' # writeBash "example" ''
# echo hello world # echo hello world
# '' # ''
writeBash = makeScriptWriter { writeBash = makeScriptWriter { interpreter = "${lib.getExe pkgs.bash}"; };
interpreter = "${lib.getExe pkgs.bash}";
};
# Like writeScriptBin but the first line is a shebang to bash # Like writeScriptBin but the first line is a shebang to bash
writeBashBin = name: writeBashBin = name: writeBash "/bin/${name}";
writeBash "/bin/${name}";
# Like writeScript but the first line is a shebang to dash # Like writeScript but the first line is a shebang to dash
# #
@@ -149,13 +166,10 @@ rec {
# writeDash "example" '' # writeDash "example" ''
# echo hello world # echo hello world
# '' # ''
writeDash = makeScriptWriter { writeDash = makeScriptWriter { interpreter = "${lib.getExe pkgs.dash}"; };
interpreter = "${lib.getExe pkgs.dash}";
};
# Like writeScriptBin but the first line is a shebang to dash # Like writeScriptBin but the first line is a shebang to dash
writeDashBin = name: writeDashBin = name: writeDash "/bin/${name}";
writeDash "/bin/${name}";
# Like writeScript but the first line is a shebang to fish # Like writeScript but the first line is a shebang to fish
# #
@@ -169,8 +183,7 @@ rec {
}; };
# Like writeScriptBin but the first line is a shebang to fish # Like writeScriptBin but the first line is a shebang to fish
writeFishBin = name: writeFishBin = name: writeFish "/bin/${name}";
writeFish "/bin/${name}";
# writeHaskell takes a name, an attrset with libraries and haskell version (both optional) # writeHaskell takes a name, an attrset with libraries and haskell version (both optional)
# and some haskell source code and returns an executable. # and some haskell source code and returns an executable.
@@ -181,18 +194,21 @@ rec {
# #
# main = launchMissiles # main = launchMissiles
# ''; # '';
writeHaskell = name: { writeHaskell =
name:
{
libraries ? [ ], libraries ? [ ],
ghc ? pkgs.ghc, ghc ? pkgs.ghc,
ghcArgs ? [ ], ghcArgs ? [ ],
threadedRuntime ? true, threadedRuntime ? true,
strip ? true strip ? true,
}: }:
let let
appendIfNotSet = el: list: if elem el list then list else list ++ [ el ]; appendIfNotSet = el: list: if elem el list then list else list ++ [ el ];
ghcArgs' = if threadedRuntime then appendIfNotSet "-threaded" ghcArgs else ghcArgs; ghcArgs' = if threadedRuntime then appendIfNotSet "-threaded" ghcArgs else ghcArgs;
in makeBinWriter { in
makeBinWriter {
compileScript = '' compileScript = ''
cp $contentPath tmp.hs cp $contentPath tmp.hs
${(ghc.withPackages (_: libraries))}/bin/ghc ${lib.escapeShellArgs ghcArgs'} tmp.hs ${(ghc.withPackages (_: libraries))}/bin/ghc ${lib.escapeShellArgs ghcArgs'} tmp.hs
@@ -202,8 +218,7 @@ rec {
} name; } name;
# writeHaskellBin takes the same arguments as writeHaskell but outputs a directory (like writeScriptBin) # writeHaskellBin takes the same arguments as writeHaskell but outputs a directory (like writeScriptBin)
writeHaskellBin = name: writeHaskellBin = name: writeHaskell "/bin/${name}";
writeHaskell "/bin/${name}";
# Like writeScript but the first line is a shebang to nu # Like writeScript but the first line is a shebang to nu
# #
@@ -211,24 +226,24 @@ rec {
# writeNu "example" '' # writeNu "example" ''
# echo hello world # echo hello world
# '' # ''
writeNu = makeScriptWriter { writeNu = makeScriptWriter { interpreter = "${lib.getExe pkgs.nushell} --no-config-file"; };
interpreter = "${lib.getExe pkgs.nushell} --no-config-file";
};
# Like writeScriptBin but the first line is a shebang to nu # Like writeScriptBin but the first line is a shebang to nu
writeNuBin = name: writeNuBin = name: writeNu "/bin/${name}";
writeNu "/bin/${name}";
# makeRubyWriter takes ruby and compatible rubyPackages and produces ruby script writer, # 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. # If any libraries are specified, ruby.withPackages is used as interpreter, otherwise the "bare" ruby is used.
makeRubyWriter = ruby: rubyPackages: buildRubyPackages: name: { libraries ? [], ... } @ args: makeRubyWriter =
ruby: _rubyPackages: _buildRubyPackages: name:
{
libraries ? [ ],
...
}@args:
makeScriptWriter ( makeScriptWriter (
(builtins.removeAttrs args [ "libraries" ]) (builtins.removeAttrs args [ "libraries" ])
// { // {
interpreter = interpreter =
if libraries == [] if libraries == [ ] then "${ruby}/bin/ruby" else "${(ruby.withPackages (_ps: libraries))}/bin/ruby";
then "${ruby}/bin/ruby"
else "${(ruby.withPackages (ps: libraries))}/bin/ruby";
# Rubocop doesnt seem to like running in this fashion. # Rubocop doesnt seem to like running in this fashion.
#check = (writeDash "rubocop.sh" '' #check = (writeDash "rubocop.sh" ''
# exec ${lib.getExe buildRubyPackages.rubocop} "$1" # exec ${lib.getExe buildRubyPackages.rubocop} "$1"
@@ -244,13 +259,14 @@ rec {
# '' # ''
writeRuby = makeRubyWriter pkgs.ruby pkgs.rubyPackages buildPackages.rubyPackages; writeRuby = makeRubyWriter pkgs.ruby pkgs.rubyPackages buildPackages.rubyPackages;
writeRubyBin = name: writeRubyBin = name: writeRuby "/bin/${name}";
writeRuby "/bin/${name}";
# makeLuaWriter takes lua and compatible luaPackages and produces lua script writer, # 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, # 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. # lua.withPackages is used as interpreter, otherwise the "bare" lua is used.
makeLuaWriter = lua: luaPackages: buildLuaPackages: name: { libraries ? [], ... } @ args: makeLuaWriter =
lua: _luaPackages: buildLuaPackages: name:
{ ... }@args:
makeScriptWriter ( makeScriptWriter (
(builtins.removeAttrs args [ "libraries" ]) (builtins.removeAttrs args [ "libraries" ])
// { // {
@@ -259,9 +275,11 @@ rec {
# then lua.interpreter # then lua.interpreter
# else (lua.withPackages (ps: libraries)).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. # This should support packages! I just cant figure out why some dependency collision happens whenever I try to run this.
check = (writeDash "luacheck.sh" '' check = (
writeDash "luacheck.sh" ''
exec ${buildLuaPackages.luacheck}/bin/luacheck "$1" exec ${buildLuaPackages.luacheck}/bin/luacheck "$1"
''); ''
);
} }
) name; ) name;
@@ -287,13 +305,14 @@ rec {
# '' # ''
writeLua = makeLuaWriter pkgs.lua pkgs.luaPackages buildPackages.luaPackages; writeLua = makeLuaWriter pkgs.lua pkgs.luaPackages buildPackages.luaPackages;
writeLuaBin = name: writeLuaBin = name: writeLua "/bin/${name}";
writeLua "/bin/${name}";
writeRust = name: { writeRust =
name:
{
rustc ? pkgs.rustc, rustc ? pkgs.rustc,
rustcArgs ? [ ], rustcArgs ? [ ],
strip ? true strip ? true,
}: }:
let let
darwinArgs = lib.optionals stdenv.isDarwin [ "-L${lib.getLib libiconv}/lib" ]; darwinArgs = lib.optionals stdenv.isDarwin [ "-L${lib.getLib libiconv}/lib" ];
@@ -306,8 +325,7 @@ rec {
inherit strip; inherit strip;
} name; } name;
writeRustBin = name: writeRustBin = name: writeRust "/bin/${name}";
writeRust "/bin/${name}";
# writeJS takes a name an attributeset with libraries and some JavaScript sourcecode and # writeJS takes a name an attributeset with libraries and some JavaScript sourcecode and
# returns an executable # returns an executable
@@ -319,23 +337,26 @@ rec {
# var result = UglifyJS.minify(code); # var result = UglifyJS.minify(code);
# console.log(result.code); # console.log(result.code);
# '' # ''
writeJS = name: { libraries ? [] }: content: writeJS =
name:
{
libraries ? [ ],
}:
content:
let let
node-env = pkgs.buildEnv { node-env = pkgs.buildEnv {
name = "node"; name = "node";
paths = libraries; paths = libraries;
pathsToLink = [ pathsToLink = [ "/lib/node_modules" ];
"/lib/node_modules"
];
}; };
in writeDash name '' in
writeDash name ''
export NODE_PATH=${node-env}/lib/node_modules export NODE_PATH=${node-env}/lib/node_modules
exec ${lib.getExe pkgs.nodejs} ${pkgs.writeText "js" content} "$@" exec ${lib.getExe pkgs.nodejs} ${pkgs.writeText "js" content} "$@"
''; '';
# writeJSBin takes the same arguments as writeJS but outputs a directory (like writeScriptBin) # writeJSBin takes the same arguments as writeJS but outputs a directory (like writeScriptBin)
writeJSBin = name: writeJSBin = name: writeJS "/bin/${name}";
writeJS "/bin/${name}";
awkFormatNginx = builtins.toFile "awkFormat-nginx.awk" '' awkFormatNginx = builtins.toFile "awkFormat-nginx.awk" ''
awk -f awk -f
@@ -345,11 +366,15 @@ rec {
{id="";for(i=idx;i<ctx;i++)id=sprintf("%s%s", id, "\t");printf "%s%s\n", id, $0} {id="";for(i=idx;i<ctx;i++)id=sprintf("%s%s", id, "\t");printf "%s%s\n", id, $0}
''; '';
writeNginxConfig = name: text: pkgs.runCommandLocal name { writeNginxConfig =
name: text:
pkgs.runCommandLocal name
{
inherit text; inherit text;
passAsFile = [ "text" ]; passAsFile = [ "text" ];
nativeBuildInputs = [ gixy ]; nativeBuildInputs = [ gixy ];
} /* sh */ '' } # sh
''
# nginx-config-formatter has an error - https://github.com/1connect/nginx-config-formatter/issues/16 # nginx-config-formatter has an error - https://github.com/1connect/nginx-config-formatter/issues/16
awk -f ${awkFormatNginx} "$textPath" | sed '/^\s*$/d' > $out awk -f ${awkFormatNginx} "$textPath" | sed '/^\s*$/d' > $out
gixy $out gixy $out
@@ -363,42 +388,55 @@ rec {
# use boolean; # use boolean;
# print "Howdy!\n" if true; # print "Howdy!\n" if true;
# '' # ''
writePerl = name: { libraries ? [], ... } @ args: writePerl =
name:
{
libraries ? [ ],
...
}@args:
makeScriptWriter ( makeScriptWriter (
(builtins.removeAttrs args [ "libraries" ]) (builtins.removeAttrs args [ "libraries" ])
// { // {
interpreter = "${lib.getExe (pkgs.perl.withPackages (p: libraries))}"; interpreter = "${lib.getExe (pkgs.perl.withPackages (_p: libraries))}";
} }
) name; ) name;
# writePerlBin takes the same arguments as writePerl but outputs a directory (like writeScriptBin) # writePerlBin takes the same arguments as writePerl but outputs a directory (like writeScriptBin)
writePerlBin = name: writePerlBin = name: writePerl "/bin/${name}";
writePerl "/bin/${name}";
# makePythonWriter takes python and compatible pythonPackages and produces python script writer, # 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, # 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. # python.withPackages is used as interpreter, otherwise the "bare" python is used.
makePythonWriter = python: pythonPackages: buildPythonPackages: name: { libraries ? [], flakeIgnore ? [], ... } @ args: makePythonWriter =
python: pythonPackages: buildPythonPackages: name:
{
libraries ? [ ],
flakeIgnore ? [ ],
...
}@args:
let let
ignoreAttribute = optionalString (flakeIgnore != []) "--ignore ${concatMapStringsSep "," escapeShellArg flakeIgnore}"; ignoreAttribute =
optionalString (flakeIgnore != [ ])
"--ignore ${concatMapStringsSep "," escapeShellArg flakeIgnore}";
in in
makeScriptWriter makeScriptWriter (
( (builtins.removeAttrs args [
(builtins.removeAttrs args ["libraries" "flakeIgnore"]) "libraries"
"flakeIgnore"
])
// { // {
interpreter = interpreter =
if pythonPackages != pkgs.pypy2Packages || pythonPackages != pkgs.pypy3Packages then if pythonPackages != pkgs.pypy2Packages || pythonPackages != pkgs.pypy3Packages then
if libraries == [] if libraries == [ ] then python.interpreter else (python.withPackages (_ps: libraries)).interpreter
then python.interpreter else
else (python.withPackages (ps: libraries)).interpreter python.interpreter;
else python.interpreter check = optionalString python.isPy3k (
; writeDash "pythoncheck.sh" ''
check = optionalString python.isPy3k (writeDash "pythoncheck.sh" ''
exec ${buildPythonPackages.flake8}/bin/flake8 --show-source ${ignoreAttribute} "$1" exec ${buildPythonPackages.flake8}/bin/flake8 --show-source ${ignoreAttribute} "$1"
''); ''
);
} }
) ) name;
name;
# writePyPy2 takes a name an attributeset with libraries and some pypy2 sourcecode and # writePyPy2 takes a name an attributeset with libraries and some pypy2 sourcecode and
# returns an executable # returns an executable
@@ -415,8 +453,7 @@ rec {
writePyPy2 = makePythonWriter pkgs.pypy2 pkgs.pypy2Packages buildPackages.pypy2Packages; writePyPy2 = makePythonWriter pkgs.pypy2 pkgs.pypy2Packages buildPackages.pypy2Packages;
# writePyPy2Bin takes the same arguments as writePyPy2 but outputs a directory (like writeScriptBin) # writePyPy2Bin takes the same arguments as writePyPy2 but outputs a directory (like writeScriptBin)
writePyPy2Bin = name: writePyPy2Bin = name: writePyPy2 "/bin/${name}";
writePyPy2 "/bin/${name}";
# writePython3 takes a name an attributeset with libraries and some python3 sourcecode and # writePython3 takes a name an attributeset with libraries and some python3 sourcecode and
# returns an executable # returns an executable
@@ -433,8 +470,7 @@ rec {
writePython3 = makePythonWriter pkgs.python3 pkgs.python3Packages buildPackages.python3Packages; writePython3 = makePythonWriter pkgs.python3 pkgs.python3Packages buildPackages.python3Packages;
# writePython3Bin takes the same arguments as writePython3 but outputs a directory (like writeScriptBin) # writePython3Bin takes the same arguments as writePython3 but outputs a directory (like writeScriptBin)
writePython3Bin = name: writePython3Bin = name: writePython3 "/bin/${name}";
writePython3 "/bin/${name}";
# writePyPy3 takes a name an attributeset with libraries and some pypy3 sourcecode and # writePyPy3 takes a name an attributeset with libraries and some pypy3 sourcecode and
# returns an executable # returns an executable
@@ -451,15 +487,23 @@ rec {
writePyPy3 = makePythonWriter pkgs.pypy3 pkgs.pypy3Packages buildPackages.pypy3Packages; writePyPy3 = makePythonWriter pkgs.pypy3 pkgs.pypy3Packages buildPackages.pypy3Packages;
# writePyPy3Bin takes the same arguments as writePyPy3 but outputs a directory (like writeScriptBin) # writePyPy3Bin takes the same arguments as writePyPy3 but outputs a directory (like writeScriptBin)
writePyPy3Bin = name: writePyPy3Bin = name: writePyPy3 "/bin/${name}";
writePyPy3 "/bin/${name}";
makeFSharpWriter =
makeFSharpWriter = { dotnet-sdk ? pkgs.dotnet-sdk, fsi-flags ? "", libraries ? _: [], ... } @ args: nameOrPath: {
dotnet-sdk ? pkgs.dotnet-sdk,
fsi-flags ? "",
libraries ? _: [ ],
...
}@args:
nameOrPath:
let let
fname = last (builtins.split "/" nameOrPath); fname = last (builtins.split "/" nameOrPath);
path = if strings.hasSuffix ".fsx" nameOrPath then nameOrPath else "${nameOrPath}.fsx"; path = if strings.hasSuffix ".fsx" nameOrPath then nameOrPath else "${nameOrPath}.fsx";
_nugetDeps = mkNugetDeps { name = "${fname}-nuget-deps"; nugetDeps = libraries; }; _nugetDeps = mkNugetDeps {
name = "${fname}-nuget-deps";
nugetDeps = libraries;
};
nuget-source = mkNugetSource { nuget-source = mkNugetSource {
name = "${fname}-nuget-source"; name = "${fname}-nuget-source";
@@ -476,22 +520,28 @@ rec {
${lib.getExe dotnet-sdk} fsi --quiet --nologo --readline- ${fsi-flags} "$@" < "$script" ${lib.getExe dotnet-sdk} fsi --quiet --nologo --readline- ${fsi-flags} "$@" < "$script"
''; '';
in content: makeScriptWriter ( in
(builtins.removeAttrs args ["dotnet-sdk" "fsi-flags" "libraries"]) content:
makeScriptWriter
(
(builtins.removeAttrs args [
"dotnet-sdk"
"fsi-flags"
"libraries"
])
// { // {
interpreter = fsi; interpreter = fsi;
} }
) path )
path
'' ''
#i "nuget: ${nuget-source}/lib" #i "nuget: ${nuget-source}/lib"
${content} ${content}
exit 0 exit 0
''; '';
writeFSharp = writeFSharp = makeFSharpWriter { };
makeFSharpWriter {};
writeFSharpBin = name: writeFSharpBin = name: writeFSharp "/bin/${name}";
writeFSharp "/bin/${name}";
} }