Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions .github/rockspec.template
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
local git_ref = '$git_ref'
local modrev = '$modrev'
local specrev = '$specrev'

local repo_url = '$repo_url'

rockspec_format = '3.0'
package = '$package'
version = modrev ..'-'.. specrev

description = {
summary = '$summary',
detailed = $detailed_description,
labels = $labels,
homepage = '$homepage',
$license
}

dependencies = $dependencies

test_dependencies = $test_dependencies

source = {
url = repo_url .. '/archive/' .. git_ref .. '.zip',
dir = '$repo_name-' .. '$archive_dir_suffix',
}

if modrev == 'scm' or modrev == 'dev' then
source = {
url = repo_url:gsub('https', 'git')
}
end

build = {
type = 'builtin',
copy_directories = $copy_directories,
modules = { ["sh.env"] = "nix/env.c" }
}
3 changes: 2 additions & 1 deletion .github/workflows/luarocks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,11 @@ jobs:
- name: Get Version
run: echo "LUAROCKS_VERSION=$(git describe --abbrev=0 --tags)" >> $GITHUB_ENV
- name: Luarocks Upload
uses: nvim-neorocks/luarocks-tag-release@v7
uses: lumen-oss/luarocks-tag-release@v7
env:
LUAROCKS_API_KEY: ${{ secrets.LUAROCKS_API_KEY }}
with:
template: ".github/rockspec.template"
version: ${{ env.LUAROCKS_VERSION }}
detailed_description: |
Tiny library with syntax sugar for shell scripting in Lua
Expand Down
25 changes: 18 additions & 7 deletions .github/workflows/nix-build.yml
Original file line number Diff line number Diff line change
@@ -1,11 +1,22 @@
name: "Nix build"
name: Tests

on:
pull_request:
push:
branches: [ master ]
pull_request:
branches: [ master ]

jobs:
checks:
runs-on: ubuntu-latest
test:
strategy:
matrix:
os: [ubuntu-latest, macos-latest]
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v6
- uses: DeterminateSystems/nix-installer-action@v22
- run: nix flake check -Lv
- uses: actions/checkout@v6
with:
fetch-depth: 0
- name: Install Nix
uses: DeterminateSystems/nix-installer-action@main
- name: Run checks
run: nix flake check -Lvv --log-format bar-with-logs
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
**/result
**/result-*
**/repl-result-out
**/repl-result-*
**/.nvim.lua
**/.cache
**/compile_commands.json
Expand Down
17 changes: 5 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ It is useful when you have a short build or wrapper script that needs to deal wi

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

Alongside all that, it also includes `require('sh.env')` which works like `vim.env`

## Install

via luarocks: `luarocks install shelua`
Expand Down Expand Up @@ -247,23 +249,15 @@ inputs.shelua = {

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`.

You may import any of them for any nixpkgs lua interpreter like this if you don't want to match them up.

```nix
luaEnv = pkgs.lua5_2.withPackages (ps: [(inputs.shelua.packages.${system}.default.override { luapkgs = ps; })]);
```

It also exports overlays. See the [flake](./flake.nix) for more details.

### In addition to the library:

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

`runLuaCommand :: str -> str -> attrs or (n2l -> attrs) -> str or (n2l -> str) -> drv`

where `n2l` is [this nix to lua library](https://github.com/BirdeeHub/nixToLua)
`runLuaCommand :: str -> str -> attrs -> str -> drv`

and the rest representing:
with those arguments representing:

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

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

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

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

- 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.

Expand Down
16 changes: 4 additions & 12 deletions default.nix
Original file line number Diff line number Diff line change
@@ -1,12 +1,4 @@
{
runCommand,
lua,
luapkgs ? lua.pkgs,
...
}: luapkgs.luaLib.toLuaModule (runCommand "shelua" {
src = ./lua/sh.lua;
env_path = with builtins; (head (split "[\/][?]" (head luapkgs.lua.LuaPathSearchPaths)));
} /*bash*/ ''
mkdir -p "$out/$env_path"
cp "$src" "$out/$env_path/sh.lua"
'')
{ pkgs ? import <nixpkgs> {}, ... }: (import ./flake.nix).outputs {
self = builtins.path { path = ./.; };
inherit pkgs;
}
16 changes: 0 additions & 16 deletions flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

175 changes: 84 additions & 91 deletions flake.nix
Original file line number Diff line number Diff line change
@@ -1,105 +1,98 @@
{
description = ''
Tiny lua module to write shell scripts with lua (inspired by zserge/luash)

Also exports runLuaCommand, which is pkgs.runCommand but the command is in lua and uses shelua
'';
inputs = {
nixpkgs.url = "github:nixos/nixpkgs/nixpkgs-unstable";
n2l.url = "github:BirdeeHub/nixToLua";
};
outputs = { nixpkgs, n2l, ... }: let
forAllSys = nixpkgs.lib.genAttrs nixpkgs.lib.platforms.all;
overlay = final: prev: {
shelua = prev.callPackage ./. { lua = prev.lua5_2; };
runLuaCommand = prev.callPackage ./nix { inherit n2l; };
};
overlay1 = final: prev: {
shelua = prev.callPackage ./. { lua = prev.lua5_2; };
inputs.nixpkgs.url = "github:nixos/nixpkgs/nixpkgs-unstable";
outputs = { self, ... }@inputs: let
lib = inputs.pkgs.lib or inputs.nixpkgs.lib or (import "${inputs.nixpkgs or <nixpkgs>}/lib");
forAllSys = lib.genAttrs lib.platforms.all;
getPkgs = system: overlays: if inputs.pkgs.stdenv.hostPlatform.system or null == system then
if builtins.isList overlays && overlays != [] then
inputs.pkgs.appendOverlays overlays
else
inputs.pkgs
else
import (inputs.pkgs.path or inputs.nixpkgs or <nixpkgs>) {
inherit system;
overlays = (if builtins.isList overlays then overlays else []) ++ inputs.pkgs.overlays or [];
config = inputs.pkgs.config or {};
};
mapAttrsToList = f: attrs: builtins.attrValues (builtins.mapAttrs f attrs);
l_pkg_enum = {
lua5_1 = "lua51Packages";
lua5_2 = "lua52Packages";
lua5_3 = "lua53Packages";
lua5_4 = "lua54Packages";
lua5_5 = "lua55Packages";
luajit = "luajitPackages";
lua = "luaPackages";
};
overlay2 = final: prev: {
runLuaCommand = prev.callPackage ./nix { inherit n2l; };
APPNAME = "shelua";
overlay = final: prev: let
luaCallPackageFn = { buildLuarocksPackage, }:
buildLuarocksPackage {
pname = APPNAME;
version = "scm-1";
src = self;
};
# lua5_1 = prev.lua5_1.override { packageOverrides };
l_pkg_main = builtins.mapAttrs (
n: _: (prev.lib.attrByPath [ n "override" ] null prev) {
packageOverrides = luaself: luaprev: {
${APPNAME} = luaself.callPackage luaCallPackageFn {};
};
}
) l_pkg_enum;
# lua51Packages = final.lua5_1.pkgs;
l_pkg_sets = builtins.listToAttrs (
mapAttrsToList (
n: v: {
name = v;
value = prev.lib.attrByPath [ n "pkgs" ] null final;
}
) l_pkg_enum
);
in l_pkg_main // l_pkg_sets // {
vimPlugins = prev.vimPlugins // {
${APPNAME} = (final.neovimUtils.buildNeovimPlugin { pname = APPNAME; }).overrideAttrs {
luarocksConfig = {
lua_modules_path = "lua";
lib_modules_path = "lua";
};
};
};
};
in {
overlays.default = overlay;
overlays.shelua = overlay1;
overlays.runLuaCommand = overlay2;
legacyPackages = forAllSys (system: let
pkgs = import nixpkgs { inherit system; overlays = [ overlay2 ]; };
in {
inherit (pkgs) runLuaCommand;
});
packages = forAllSys (system: let
pkgs = import nixpkgs { inherit system; overlays = [ overlay1 ]; };
in nixpkgs.lib.pipe (with pkgs; [ lua5_1 lua5_2 lua5_3 lua5_4 luajit ]) [
(builtins.map (li: { name = "she" + li.luaAttr; value = pkgs.shelua.override { lua = li; }; }))
builtins.listToAttrs
] // {
default = pkgs.shelua;
pkgs = getPkgs system [ overlay ];
in (
with builtins; listToAttrs (
map (n: {
name = "she${n}";
value = pkgs.lib.attrByPath [ n "pkgs" APPNAME ] null pkgs;
}) (attrNames l_pkg_enum)
)
) // {
default = pkgs.vimPlugins.${APPNAME};
"vimPlugins-${APPNAME}" = pkgs.vimPlugins.${APPNAME};
});
runLuaCommandOverlay = final: prev: { runLuaCommand = final.callPackage ./nix {}; };
in {
overlays.default = overlay;
overlays.runLuaCommand = runLuaCommandOverlay;
legacyPackages = forAllSys (system: { inherit (getPkgs system [ runLuaCommandOverlay ]) runLuaCommand; });
inherit packages;
checks = forAllSys (system: import ./tests/tests.nix (getPkgs system [ overlay runLuaCommandOverlay ]));
devShells = forAllSys (system: let
pkgs = import nixpkgs { inherit system; overlays = [ overlay ]; };
pkgs = getPkgs system [];
lua = pkgs.luajit.withPackages (lp: [ lp.inspect lp.cjson lp.toml-edit lp.luarocks ]);
in {
default = pkgs.mkShell {
name = "testshell";
packages = with pkgs; [ bear ];
inputsFrom = [ ];
luaInterpreter = (pkgs.lua5_2.withPackages (ps: with ps; [inspect])).interpreter;
name = "${APPNAME}-dev";
packages = [ lua ];
LUA_INCDIR = "${lua}/include";
LUA = lua.interpreter;
BEAR = "${pkgs.bear}/bin/bear";
shellHook = ''
make_cc() {
pushd "$(git rev-parse --show-toplevel || echo ".")"
mkdir -p ./build
bear -- $CC -O2 -fPIC -shared -o ./build/env.so ./nix/env.c -I"$(dirname $luaInterpreter)/../include" "$@"
popd
}
[ "$(whoami)" == "birdee" ] && exec zsh
'';
};
});
checks = forAllSys (system: let
pkgs = import nixpkgs { inherit system; overlays = [ overlay ]; };
mkBuildTest = lua: let
luapath = (lua.withPackages (ps: with ps; [inspect (pkgs.shelua.override { luapkgs = ps; })])).interpreter;
in pkgs.runCommand ("shelua_package_test-" + lua.luaAttr) {} ''
echo 'package.path = package.path .. ";${./tests}/?.lua"; require("test")' | ${luapath} - > "$out"
'';
mkCmdTest = lua: pkgs.runLuaCommand ("runLuaCommand_test-" + lua.luaAttr) (lua.withPackages (ps: with ps; [inspect])).interpreter {
nativeBuildInputs = [ pkgs.makeWrapper ];
passthru = {
testdata = [ "some" "values" ];
notincluded = system: builtins.trace system system;
};
} /*lua*/''
local inspect = require('inspect')
local outbin = out .. "/bin"
local outfile = outbin .. "/testpkg"
local outdrv = outbin .. "/testdrv"
local outcat = outbin .. "/newcat"
local outecho = outbin .. "/newecho"
sh.mkdir("-p", outbin)
os.env.FRIEND = "everyone"
assert(os.getenv("FRIEND") == os.env.FRIEND, "os.env failed")
print(os.getenv("FRIEND"))
os.write_file({}, outfile, [[#!${pkgs.bash}/bin/bash]])
os.write_file({ append = true, }, outfile, [[echo "hello world!"]])
os.write_file({ append = true, }, outfile, [[cat ]] .. outdrv)
os.write_file({ append = true, }, outfile, outcat)
os.write_file({}, outdrv, inspect(drv))
sh.escape_args = true
sh.makeWrapper([[${pkgs.writeShellScript "testscript" ''echo "$@"''}]], outecho, "--add-flags", "testingtesting '1 2' 3")
sh.escape_args = false
sh.makeWrapper([[${pkgs.coreutils}/bin/cat]], outcat, "--add-flags", outecho)
sh.chmod("+x", outfile)
dofile("${./example.lua}")
package.path = package.path .. ";${./tests}/?.lua"
require("test")
'';
run_on = with pkgs; [ lua5_1 lua5_2 lua5_3 lua5_4 luajit ];
in nixpkgs.lib.pipe run_on [
(builtins.map (li: { name = "runLuaCommand-" + li.luaAttr; value = mkCmdTest li; }))
builtins.listToAttrs
] // (nixpkgs.lib.pipe run_on [
(builtins.map (li: { name = "withPackages-" + li.luaAttr; value = mkBuildTest li; }))
builtins.listToAttrs
]));
};
}
Loading