Skip to content

Commit 6af3ed4

Browse files
authored
Merge pull request #90 from BirdeeHub/shenv
feat(sh.env): + runLuaCommand now only translates passthru.info + ci
2 parents 84df096 + a46b961 commit 6af3ed4

16 files changed

Lines changed: 654 additions & 289 deletions

.github/rockspec.template

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
local git_ref = '$git_ref'
2+
local modrev = '$modrev'
3+
local specrev = '$specrev'
4+
5+
local repo_url = '$repo_url'
6+
7+
rockspec_format = '3.0'
8+
package = '$package'
9+
version = modrev ..'-'.. specrev
10+
11+
description = {
12+
summary = '$summary',
13+
detailed = $detailed_description,
14+
labels = $labels,
15+
homepage = '$homepage',
16+
$license
17+
}
18+
19+
dependencies = $dependencies
20+
21+
test_dependencies = $test_dependencies
22+
23+
source = {
24+
url = repo_url .. '/archive/' .. git_ref .. '.zip',
25+
dir = '$repo_name-' .. '$archive_dir_suffix',
26+
}
27+
28+
if modrev == 'scm' or modrev == 'dev' then
29+
source = {
30+
url = repo_url:gsub('https', 'git')
31+
}
32+
end
33+
34+
build = {
35+
type = 'builtin',
36+
copy_directories = $copy_directories,
37+
modules = { ["sh.env"] = "nix/env.c" }
38+
}

.github/workflows/luarocks.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,11 @@ jobs:
2323
- name: Get Version
2424
run: echo "LUAROCKS_VERSION=$(git describe --abbrev=0 --tags)" >> $GITHUB_ENV
2525
- name: Luarocks Upload
26-
uses: nvim-neorocks/luarocks-tag-release@v7
26+
uses: lumen-oss/luarocks-tag-release@v7
2727
env:
2828
LUAROCKS_API_KEY: ${{ secrets.LUAROCKS_API_KEY }}
2929
with:
30+
template: ".github/rockspec.template"
3031
version: ${{ env.LUAROCKS_VERSION }}
3132
detailed_description: |
3233
Tiny library with syntax sugar for shell scripting in Lua

.github/workflows/nix-build.yml

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,22 @@
1-
name: "Nix build"
1+
name: Tests
2+
23
on:
3-
pull_request:
44
push:
5+
branches: [ master ]
6+
pull_request:
7+
branches: [ master ]
8+
59
jobs:
6-
checks:
7-
runs-on: ubuntu-latest
10+
test:
11+
strategy:
12+
matrix:
13+
os: [ubuntu-latest, macos-latest]
14+
runs-on: ${{ matrix.os }}
815
steps:
9-
- uses: actions/checkout@v6
10-
- uses: DeterminateSystems/nix-installer-action@v22
11-
- run: nix flake check -Lv
16+
- uses: actions/checkout@v6
17+
with:
18+
fetch-depth: 0
19+
- name: Install Nix
20+
uses: DeterminateSystems/nix-installer-action@main
21+
- name: Run checks
22+
run: nix flake check -Lvv --log-format bar-with-logs

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
**/result
2+
**/result-*
23
**/repl-result-out
4+
**/repl-result-*
35
**/.nvim.lua
46
**/.cache
57
**/compile_commands.json

README.md

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ It is useful when you have a short build or wrapper script that needs to deal wi
2929

3030
Especially when you have a lot of `json` and would rather use `cjson` and deal with tables than use `jq` and bash arrays
3131

32+
Alongside all that, it also includes `require('sh.env')` which works like `vim.env`
33+
3234
## Install
3335

3436
via luarocks: `luarocks install shelua`
@@ -247,23 +249,15 @@ inputs.shelua = {
247249

248250
The library is exported by the flake under `inputs.shelua.packages.${system}` as `default`, `shelua5_1`, `shelua5_2`, `shelua5_3`, `shelua5_4`, and `sheluajit_2_1`.
249251

250-
You may import any of them for any nixpkgs lua interpreter like this if you don't want to match them up.
251-
252-
```nix
253-
luaEnv = pkgs.lua5_2.withPackages (ps: [(inputs.shelua.packages.${system}.default.override { luapkgs = ps; })]);
254-
```
255-
256252
It also exports overlays. See the [flake](./flake.nix) for more details.
257253

258254
### In addition to the library:
259255

260256
It exports a `inputs.shelua.legacyPackages.${system}.runLuaCommand` which is a lot like `pkgs.runCommand` except the command is in lua.
261257

262-
`runLuaCommand :: str -> str -> attrs or (n2l -> attrs) -> str or (n2l -> str) -> drv`
263-
264-
where `n2l` is [this nix to lua library](https://github.com/BirdeeHub/nixToLua)
258+
`runLuaCommand :: str -> str -> attrs -> str -> drv`
265259

266-
and the rest representing:
260+
with those arguments representing:
267261

268262
`runLuaCommand :: name -> lua_interpreter_path -> drvArgs -> lua_command -> drv`
269263

@@ -291,10 +285,9 @@ You should provide the interpreter path via something like this to get the most
291285

292286
- `os.write_file(opts, filename, contents)` will be added where opts is `{ append = false, newline = true }` by default
293287

294-
- `os.env` will be added. Setting values in the table will set the environment variable
288+
- `os.env` will be added as an alias for `require('sh.env')` Setting values in the table will set the environment variable
295289
in the process environment, setting one to nil will unset it,
296290
reading will return the environment variable's value.
297-
Analogous to `vim.env` in neovim.
298291

299292
- The path to the shell hooks will be added to the shelua library's metatable (via `sh.stdenv_shell_hooks_path = shell_hooks`), for use in redefining the existing transform which adds them if desired.
300293

default.nix

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,4 @@
1-
{
2-
runCommand,
3-
lua,
4-
luapkgs ? lua.pkgs,
5-
...
6-
}: luapkgs.luaLib.toLuaModule (runCommand "shelua" {
7-
src = ./lua/sh.lua;
8-
env_path = with builtins; (head (split "[\/][?]" (head luapkgs.lua.LuaPathSearchPaths)));
9-
} /*bash*/ ''
10-
mkdir -p "$out/$env_path"
11-
cp "$src" "$out/$env_path/sh.lua"
12-
'')
1+
{ pkgs ? import <nixpkgs> {}, ... }: (import ./flake.nix).outputs {
2+
self = builtins.path { path = ./.; };
3+
inherit pkgs;
4+
}

flake.lock

Lines changed: 0 additions & 16 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

flake.nix

Lines changed: 84 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -1,105 +1,98 @@
11
{
2-
description = ''
3-
Tiny lua module to write shell scripts with lua (inspired by zserge/luash)
4-
5-
Also exports runLuaCommand, which is pkgs.runCommand but the command is in lua and uses shelua
6-
'';
7-
inputs = {
8-
nixpkgs.url = "github:nixos/nixpkgs/nixpkgs-unstable";
9-
n2l.url = "github:BirdeeHub/nixToLua";
10-
};
11-
outputs = { nixpkgs, n2l, ... }: let
12-
forAllSys = nixpkgs.lib.genAttrs nixpkgs.lib.platforms.all;
13-
overlay = final: prev: {
14-
shelua = prev.callPackage ./. { lua = prev.lua5_2; };
15-
runLuaCommand = prev.callPackage ./nix { inherit n2l; };
16-
};
17-
overlay1 = final: prev: {
18-
shelua = prev.callPackage ./. { lua = prev.lua5_2; };
2+
inputs.nixpkgs.url = "github:nixos/nixpkgs/nixpkgs-unstable";
3+
outputs = { self, ... }@inputs: let
4+
lib = inputs.pkgs.lib or inputs.nixpkgs.lib or (import "${inputs.nixpkgs or <nixpkgs>}/lib");
5+
forAllSys = lib.genAttrs lib.platforms.all;
6+
getPkgs = system: overlays: if inputs.pkgs.stdenv.hostPlatform.system or null == system then
7+
if builtins.isList overlays && overlays != [] then
8+
inputs.pkgs.appendOverlays overlays
9+
else
10+
inputs.pkgs
11+
else
12+
import (inputs.pkgs.path or inputs.nixpkgs or <nixpkgs>) {
13+
inherit system;
14+
overlays = (if builtins.isList overlays then overlays else []) ++ inputs.pkgs.overlays or [];
15+
config = inputs.pkgs.config or {};
16+
};
17+
mapAttrsToList = f: attrs: builtins.attrValues (builtins.mapAttrs f attrs);
18+
l_pkg_enum = {
19+
lua5_1 = "lua51Packages";
20+
lua5_2 = "lua52Packages";
21+
lua5_3 = "lua53Packages";
22+
lua5_4 = "lua54Packages";
23+
lua5_5 = "lua55Packages";
24+
luajit = "luajitPackages";
25+
lua = "luaPackages";
1926
};
20-
overlay2 = final: prev: {
21-
runLuaCommand = prev.callPackage ./nix { inherit n2l; };
27+
APPNAME = "shelua";
28+
overlay = final: prev: let
29+
luaCallPackageFn = { buildLuarocksPackage, }:
30+
buildLuarocksPackage {
31+
pname = APPNAME;
32+
version = "scm-1";
33+
src = self;
34+
};
35+
# lua5_1 = prev.lua5_1.override { packageOverrides };
36+
l_pkg_main = builtins.mapAttrs (
37+
n: _: (prev.lib.attrByPath [ n "override" ] null prev) {
38+
packageOverrides = luaself: luaprev: {
39+
${APPNAME} = luaself.callPackage luaCallPackageFn {};
40+
};
41+
}
42+
) l_pkg_enum;
43+
# lua51Packages = final.lua5_1.pkgs;
44+
l_pkg_sets = builtins.listToAttrs (
45+
mapAttrsToList (
46+
n: v: {
47+
name = v;
48+
value = prev.lib.attrByPath [ n "pkgs" ] null final;
49+
}
50+
) l_pkg_enum
51+
);
52+
in l_pkg_main // l_pkg_sets // {
53+
vimPlugins = prev.vimPlugins // {
54+
${APPNAME} = (final.neovimUtils.buildNeovimPlugin { pname = APPNAME; }).overrideAttrs {
55+
luarocksConfig = {
56+
lua_modules_path = "lua";
57+
lib_modules_path = "lua";
58+
};
59+
};
60+
};
2261
};
23-
in {
24-
overlays.default = overlay;
25-
overlays.shelua = overlay1;
26-
overlays.runLuaCommand = overlay2;
27-
legacyPackages = forAllSys (system: let
28-
pkgs = import nixpkgs { inherit system; overlays = [ overlay2 ]; };
29-
in {
30-
inherit (pkgs) runLuaCommand;
31-
});
3262
packages = forAllSys (system: let
33-
pkgs = import nixpkgs { inherit system; overlays = [ overlay1 ]; };
34-
in nixpkgs.lib.pipe (with pkgs; [ lua5_1 lua5_2 lua5_3 lua5_4 luajit ]) [
35-
(builtins.map (li: { name = "she" + li.luaAttr; value = pkgs.shelua.override { lua = li; }; }))
36-
builtins.listToAttrs
37-
] // {
38-
default = pkgs.shelua;
63+
pkgs = getPkgs system [ overlay ];
64+
in (
65+
with builtins; listToAttrs (
66+
map (n: {
67+
name = "she${n}";
68+
value = pkgs.lib.attrByPath [ n "pkgs" APPNAME ] null pkgs;
69+
}) (attrNames l_pkg_enum)
70+
)
71+
) // {
72+
default = pkgs.vimPlugins.${APPNAME};
73+
"vimPlugins-${APPNAME}" = pkgs.vimPlugins.${APPNAME};
3974
});
75+
runLuaCommandOverlay = final: prev: { runLuaCommand = final.callPackage ./nix {}; };
76+
in {
77+
overlays.default = overlay;
78+
overlays.runLuaCommand = runLuaCommandOverlay;
79+
legacyPackages = forAllSys (system: { inherit (getPkgs system [ runLuaCommandOverlay ]) runLuaCommand; });
80+
inherit packages;
81+
checks = forAllSys (system: import ./tests/tests.nix (getPkgs system [ overlay runLuaCommandOverlay ]));
4082
devShells = forAllSys (system: let
41-
pkgs = import nixpkgs { inherit system; overlays = [ overlay ]; };
83+
pkgs = getPkgs system [];
84+
lua = pkgs.luajit.withPackages (lp: [ lp.inspect lp.cjson lp.toml-edit lp.luarocks ]);
4285
in {
4386
default = pkgs.mkShell {
44-
name = "testshell";
45-
packages = with pkgs; [ bear ];
46-
inputsFrom = [ ];
47-
luaInterpreter = (pkgs.lua5_2.withPackages (ps: with ps; [inspect])).interpreter;
87+
name = "${APPNAME}-dev";
88+
packages = [ lua ];
89+
LUA_INCDIR = "${lua}/include";
90+
LUA = lua.interpreter;
91+
BEAR = "${pkgs.bear}/bin/bear";
4892
shellHook = ''
49-
make_cc() {
50-
pushd "$(git rev-parse --show-toplevel || echo ".")"
51-
mkdir -p ./build
52-
bear -- $CC -O2 -fPIC -shared -o ./build/env.so ./nix/env.c -I"$(dirname $luaInterpreter)/../include" "$@"
53-
popd
54-
}
93+
[ "$(whoami)" == "birdee" ] && exec zsh
5594
'';
5695
};
5796
});
58-
checks = forAllSys (system: let
59-
pkgs = import nixpkgs { inherit system; overlays = [ overlay ]; };
60-
mkBuildTest = lua: let
61-
luapath = (lua.withPackages (ps: with ps; [inspect (pkgs.shelua.override { luapkgs = ps; })])).interpreter;
62-
in pkgs.runCommand ("shelua_package_test-" + lua.luaAttr) {} ''
63-
echo 'package.path = package.path .. ";${./tests}/?.lua"; require("test")' | ${luapath} - > "$out"
64-
'';
65-
mkCmdTest = lua: pkgs.runLuaCommand ("runLuaCommand_test-" + lua.luaAttr) (lua.withPackages (ps: with ps; [inspect])).interpreter {
66-
nativeBuildInputs = [ pkgs.makeWrapper ];
67-
passthru = {
68-
testdata = [ "some" "values" ];
69-
notincluded = system: builtins.trace system system;
70-
};
71-
} /*lua*/''
72-
local inspect = require('inspect')
73-
local outbin = out .. "/bin"
74-
local outfile = outbin .. "/testpkg"
75-
local outdrv = outbin .. "/testdrv"
76-
local outcat = outbin .. "/newcat"
77-
local outecho = outbin .. "/newecho"
78-
sh.mkdir("-p", outbin)
79-
os.env.FRIEND = "everyone"
80-
assert(os.getenv("FRIEND") == os.env.FRIEND, "os.env failed")
81-
print(os.getenv("FRIEND"))
82-
os.write_file({}, outfile, [[#!${pkgs.bash}/bin/bash]])
83-
os.write_file({ append = true, }, outfile, [[echo "hello world!"]])
84-
os.write_file({ append = true, }, outfile, [[cat ]] .. outdrv)
85-
os.write_file({ append = true, }, outfile, outcat)
86-
os.write_file({}, outdrv, inspect(drv))
87-
sh.escape_args = true
88-
sh.makeWrapper([[${pkgs.writeShellScript "testscript" ''echo "$@"''}]], outecho, "--add-flags", "testingtesting '1 2' 3")
89-
sh.escape_args = false
90-
sh.makeWrapper([[${pkgs.coreutils}/bin/cat]], outcat, "--add-flags", outecho)
91-
sh.chmod("+x", outfile)
92-
dofile("${./example.lua}")
93-
package.path = package.path .. ";${./tests}/?.lua"
94-
require("test")
95-
'';
96-
run_on = with pkgs; [ lua5_1 lua5_2 lua5_3 lua5_4 luajit ];
97-
in nixpkgs.lib.pipe run_on [
98-
(builtins.map (li: { name = "runLuaCommand-" + li.luaAttr; value = mkCmdTest li; }))
99-
builtins.listToAttrs
100-
] // (nixpkgs.lib.pipe run_on [
101-
(builtins.map (li: { name = "withPackages-" + li.luaAttr; value = mkBuildTest li; }))
102-
builtins.listToAttrs
103-
]));
10497
};
10598
}

0 commit comments

Comments
 (0)