Merge pull request #281039 from tulilirockz/feat-misc-writers

writers: support for more writers
This commit is contained in:
Lassulus 2024-01-30 13:34:52 +07:00 committed by GitHub
commit e8c3dda168
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 174 additions and 48 deletions

View file

@ -1,5 +1,6 @@
{ config, lib, callPackages }:
# If you are reading this, you can test these writers by running: nix-build . -A tests.writers
let
aliases = if config.allowAliases then (import ./aliases.nix lib) else prev: {};

View file

@ -13,7 +13,7 @@ let
in
rec {
# Base implementation for non-compiled executables.
# Takes an interpreter, for example `${pkgs.bash}/bin/bash`
# Takes an interpreter, for example `${lib.getExe pkgs.bash}`
#
# Examples:
# writeBash = makeScriptWriter { interpreter = "${pkgs.bash}/bin/bash"; }
@ -116,7 +116,7 @@ rec {
# echo hello world
# ''
writeBash = makeScriptWriter {
interpreter = "${pkgs.bash}/bin/bash";
interpreter = "${lib.getExe pkgs.bash}";
};
# Like writeScriptBin but the first line is a shebang to bash
@ -130,7 +130,7 @@ rec {
# echo hello world
# ''
writeDash = makeScriptWriter {
interpreter = "${pkgs.dash}/bin/dash";
interpreter = "${lib.getExe pkgs.dash}";
};
# Like writeScriptBin but the first line is a shebang to dash
@ -144,8 +144,8 @@ rec {
# echo hello world
# ''
writeFish = makeScriptWriter {
interpreter = "${pkgs.fish}/bin/fish --no-config";
check = "${pkgs.fish}/bin/fish --no-config --no-execute"; # syntax check only
interpreter = "${lib.getExe pkgs.fish} --no-config";
check = "${lib.getExe pkgs.fish} --no-config --no-execute"; # syntax check only
};
# Like writeScriptBin but the first line is a shebang to fish
@ -175,7 +175,7 @@ rec {
in makeBinWriter {
compileScript = ''
cp $contentPath tmp.hs
${ghc.withPackages (_: libraries )}/bin/ghc ${lib.escapeShellArgs ghcArgs'} tmp.hs
${(ghc.withPackages (_: libraries ))}/bin/ghc ${lib.escapeShellArgs ghcArgs'} tmp.hs
mv tmp $out
'';
inherit strip;
@ -185,6 +185,85 @@ rec {
writeHaskellBin = name:
writeHaskell "/bin/${name}";
# Like writeScript but the first line is a shebang to nu
#
# Example:
# writeNu "example" ''
# echo hello world
# ''
writeNu = makeScriptWriter {
interpreter = "${lib.getExe pkgs.nushell} --no-config-file";
};
# Like writeScriptBin but the first line is a shebang to nu
writeNuBin = name:
writeNu "/bin/${name}";
# 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.
makeRubyWriter = ruby: rubyPackages: buildRubyPackages: name: { libraries ? [], }:
makeScriptWriter {
interpreter =
if libraries == []
then "${ruby}/bin/ruby"
else "${(ruby.withPackages (ps: libraries))}/bin/ruby";
# Rubocop doesnt seem to like running in this fashion.
#check = (writeDash "rubocop.sh" ''
# exec ${lib.getExe buildRubyPackages.rubocop} "$1"
#'');
} name;
# Like writeScript but the first line is a shebang to ruby
#
# Example:
# writeRuby "example" ''
# puts "hello world"
# ''
writeRuby = makeRubyWriter pkgs.ruby pkgs.rubyPackages buildPackages.rubyPackages;
writeRubyBin = name:
writeRuby "/bin/${name}";
# 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.
makeLuaWriter = lua: luaPackages: buildLuaPackages: name: { libraries ? [], }:
makeScriptWriter {
interpreter = lua.interpreter;
# 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.
check = (writeDash "luacheck.sh" ''
exec ${buildLuaPackages.luacheck}/bin/luacheck "$1"
'');
} name;
# writeLua takes a name an attributeset with libraries and some lua source code and
# returns an executable (should also work with luajit)
#
# Example:
# 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;
writeLuaBin = name:
writeLua "/bin/${name}";
writeRust = name: {
rustc ? pkgs.rustc,
rustcArgs ? [],
@ -196,7 +275,7 @@ rec {
makeBinWriter {
compileScript = ''
cp "$contentPath" tmp.rs
PATH=${lib.makeBinPath [pkgs.gcc]} ${lib.getBin rustc}/bin/rustc ${lib.escapeShellArgs rustcArgs} ${lib.escapeShellArgs darwinArgs} -o "$out" tmp.rs
PATH=${lib.makeBinPath [pkgs.gcc]} ${rustc}/bin/rustc ${lib.escapeShellArgs rustcArgs} ${lib.escapeShellArgs darwinArgs} -o "$out" tmp.rs
'';
inherit strip;
} name;
@ -225,7 +304,7 @@ rec {
};
in writeDash name ''
export NODE_PATH=${node-env}/lib/node_modules
exec ${pkgs.nodejs}/bin/node ${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)
@ -260,7 +339,7 @@ rec {
# ''
writePerl = name: { libraries ? [] }:
makeScriptWriter {
interpreter = "${pkgs.perl.withPackages (p: libraries)}/bin/perl";
interpreter = "${lib.getExe (pkgs.perl.withPackages (p: libraries))}";
} name;
# writePerlBin takes the same arguments as writePerl but outputs a directory (like writeScriptBin)
@ -276,9 +355,11 @@ rec {
in
makeScriptWriter {
interpreter =
if libraries == []
then python.interpreter
else (python.withPackages (ps: libraries)).interpreter
if pythonPackages != pkgs.pypy2Packages || pythonPackages != pkgs.pypy3Packages then
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"
@ -358,7 +439,7 @@ rec {
export DOTNET_CLI_TELEMETRY_OPTOUT=1
export DOTNET_NOLOGO=1
script="$1"; shift
${dotnet-sdk}/bin/dotnet fsi --quiet --nologo --readline- ${fsi-flags} "$@" < "$script"
${lib.getExe dotnet-sdk} fsi --quiet --nologo --readline- ${fsi-flags} "$@" < "$script"
'';
in content: makeScriptWriter {

View file

@ -6,11 +6,16 @@
, pypy2Packages
, python3Packages
, pypy3Packages
, luaPackages
, rubyPackages
, runCommand
, testers
, writers
, writeText
}:
# If you are reading this, you can test these writers by running: nix-build . -A tests.writers
with writers;
let
expectSuccess = test:
@ -88,15 +93,6 @@ lib.recurseIntoAttrs {
print "success\n" if true;
'');
pypy2 = expectSuccessBin (writePyPy2Bin "test-writers-pypy2-bin" { libraries = [ pypy2Packages.enum ]; } ''
from enum import Enum
class Test(Enum):
a = "success"
print Test.a
'');
python3 = expectSuccessBin (writePython3Bin "test-writers-python3-bin" { libraries = [ python3Packages.pyyaml ]; } ''
import yaml
@ -106,14 +102,47 @@ lib.recurseIntoAttrs {
print(y[0]['test'])
'');
pypy3 = expectSuccessBin (writePyPy3Bin "test-writers-pypy3-bin" { libraries = [ pypy3Packages.pyyaml ]; } ''
import yaml
# Commented out because of this issue: https://github.com/NixOS/nixpkgs/issues/39356
y = yaml.safe_load("""
- test: success
""")
print(y[0]['test'])
'');
#pypy2 = expectSuccessBin (writePyPy2Bin "test-writers-pypy2-bin" { libraries = [ pypy2Packages.enum ]; } ''
# from enum import Enum
#
# class Test(Enum):
# a = "success"
#
# print Test.a
#'');
#pypy3 = expectSuccessBin (writePyPy3Bin "test-writers-pypy3-bin" { libraries = [ pypy3Packages.pyyaml ]; } ''
# import yaml
#
# y = yaml.safe_load("""
# - test: success
# """)
# print(y[0]['test'])
#'');
# Could not test this because of external package issues :(
#lua = writeLuaBin "test-writers-lua-bin" { 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!
#'';
#ruby = expectSuccessBin (writeRubyBin "test-writers-ruby-bin" { libraries = [ rubyPackages.rubocop ]; } ''
#puts "This should work!"
#'');
};
simple = lib.recurseIntoAttrs {
@ -131,6 +160,10 @@ lib.recurseIntoAttrs {
end
'');
nu = expectSuccess (writeNu "test-writers-nushell" ''
echo "success"
'');
haskell = expectSuccess (writeHaskell "test-writers-haskell" { libraries = [ haskellPackages.acme-default ]; } ''
import Data.Default
@ -158,15 +191,6 @@ lib.recurseIntoAttrs {
print "success\n" if true;
'');
pypy2 = expectSuccess (writePyPy2 "test-writers-pypy2" { libraries = [ pypy2Packages.enum ]; } ''
from enum import Enum
class Test(Enum):
a = "success"
print Test.a
'');
python3 = expectSuccess (writePython3 "test-writers-python3" { libraries = [ python3Packages.pyyaml ]; } ''
import yaml
@ -176,14 +200,25 @@ lib.recurseIntoAttrs {
print(y[0]['test'])
'');
pypy3 = expectSuccess (writePyPy3 "test-writers-pypy3" { libraries = [ pypy3Packages.pyyaml ]; } ''
import yaml
# Commented out because of this issue: https://github.com/NixOS/nixpkgs/issues/39356
y = yaml.safe_load("""
- test: success
""")
print(y[0]['test'])
'');
#pypy2 = expectSuccessBin (writePyPy2Bin "test-writers-pypy2-bin" { libraries = [ pypy2Packages.enum ]; } ''
# from enum import Enum
#
# class Test(Enum):
# a = "success"
#
# print Test.a
#'');
#pypy3 = expectSuccessBin (writePyPy3Bin "test-writers-pypy3-bin" { libraries = [ pypy3Packages.pyyaml ]; } ''
# import yaml
#
# y = yaml.safe_load("""
# - test: success
# """)
# print(y[0]['test'])
#'');
fsharp = expectSuccess (makeFSharpWriter {
libraries = { fetchNuGet }: [
@ -191,6 +226,7 @@ lib.recurseIntoAttrs {
(fetchNuGet { pname = "System.Text.Json"; version = "4.6.0"; sha256 = "0ism236hwi0k6axssfq58s1d8lihplwiz058pdvl8al71hagri39"; })
];
} "test-writers-fsharp" ''
#r "nuget: FSharp.SystemTextJson, 0.17.4"
module Json =
@ -209,9 +245,9 @@ lib.recurseIntoAttrs {
|> printfn "%s"
'');
pypy2NoLibs = expectSuccess (writePyPy2 "test-writers-pypy2-no-libs" {} ''
print("success")
'');
#pypy2NoLibs = expectSuccess (writePyPy2 "test-writers-pypy2-no-libs" {} ''
# print("success")
#'');
python3NoLibs = expectSuccess (writePython3 "test-writers-python3-no-libs" {} ''
print("success")
@ -223,6 +259,14 @@ lib.recurseIntoAttrs {
fsharpNoNugetDeps = expectSuccess (writeFSharp "test-writers-fsharp-no-nuget-deps" ''
printfn "success"
'');
luaNoLibs = expectSuccess (writeLua "test-writers-lua-no-libs" {} ''
print("success")
'');
rubyNoLibs = expectSuccess (writeRuby "test-writers-ruby-no-libs" {} ''
puts "success"
'');
};