lib: segregate and deprecate functions that need pkgs

Splits everything that depends on a `pkgs` instance into an optional
attrs, allowing `helpers.nix` to be bootstrapped without `pkgs`.

This required some refactoring:
- `modules.specialArgs` is only available when `pkgs` is used
- `modules.specialArgsWith` now requires `defaultPkgs` be provided
- `builders.*` now have `*With` variants that take `pkgs` as an argument
  and a `withPkgs` function that returns the old interface
- Had to define the fixed part of `builders` outside the attrs for now,
  to avoid infinite recursion.
- The old `builders` are now deprecated, and print a warning when
  evaluated
- `withOptoinalFns` was introduced to merge the optional attrs into the
  final lib.
This commit is contained in:
Matt Sturgeon 2024-09-12 14:56:36 +01:00
parent f47e8f8f79
commit 4e5bd1d79b
No known key found for this signature in database
GPG key ID: 4F91844CED1A8299
12 changed files with 165 additions and 84 deletions

View file

@ -1,21 +1,39 @@
{ lib, pkgs }:
rec {
{ lib }:
# NOTE: use local recursion instead of accessing `lib.nixvim.builders`.
# The latter isn't a fixed shape since it may get the deprecated functions meregd in,
# which would lead to infinite recursion.
lib.fix (builders: {
# Curry a nixpkgs instance into the *With functions below, dropping the `With` suffix
withPkgs =
pkgs:
lib.concatMapAttrs (
name: fn:
let
match' = builtins.match "(.*)With" name;
name' = builtins.head match';
in
lib.optionalAttrs (match' != null) {
${name'} = fn pkgs;
}
) builders;
/*
Write a lua file to the nix store, formatted using stylua.
# Type
```
writeLua :: String -> String -> Derivation
writeLuaWith :: pkgs -> String -> String -> Derivation
```
# Arguments
- [pkgs] A nixpkgs instance
- [name] The name of the derivation
- [text] The content of the lua file
*/
writeLua =
name: text:
writeLuaWith =
pkgs: name: text:
pkgs.runCommand name { inherit text; } ''
echo -n "$text" > "$out"
@ -33,16 +51,17 @@ rec {
# Type
```
writeByteCompiledLua :: String -> String -> Derivation
writeByteCompiledLuaWith :: pkgs -> String -> String -> Derivation
```
# Arguments
- [pkgs] A nixpkgs instance
- [name] The name of the derivation
- [text] The content of the lua file
*/
writeByteCompiledLua =
name: text:
writeByteCompiledLuaWith =
pkgs: name: text:
pkgs.runCommandLocal name { inherit text; } ''
echo -n "$text" > "$out"
@ -56,54 +75,57 @@ rec {
# Type
```
byteCompileLuaFile :: String -> String -> Derivation
byteCompileLuaFileWith :: pkgs -> String -> String -> Derivation
```
# Arguments
- [pkgs] A nixpkgs instance
- [name] The name of the derivation
- [src] The path to the source lua file
*/
byteCompileLuaFile =
name: src:
byteCompileLuaFileWith =
pkgs: name: src:
pkgs.runCommandLocal name { inherit src; } ''
${lib.getExe' pkgs.luajit "luajit"} -bd -- "$src" "$out"
'';
# Setup hook to byte compile all lua files in the output directory.
# Invalid lua files are ignored.
byteCompileLuaHook = pkgs.makeSetupHook { name = "byte-compile-lua-hook"; } (
let
luajit = lib.getExe' pkgs.luajit "luajit";
in
pkgs.writeText "byte-compile-lua-hook.sh" # bash
''
byteCompileLuaPostFixup() {
# Target is a single file
if [[ -f $out ]]; then
if [[ $out = *.lua ]]; then
tmp=$(mktemp)
${luajit} -bd -- "$out" "$tmp"
mv "$tmp" "$out"
fi
return
fi
byteCompileLuaHookWith =
pkgs:
pkgs.makeSetupHook { name = "byte-compile-lua-hook"; } (
let
luajit = lib.getExe' pkgs.luajit "luajit";
in
pkgs.writeText "byte-compile-lua-hook.sh" # bash
''
byteCompileLuaPostFixup() {
# Target is a single file
if [[ -f $out ]]; then
if [[ $out = *.lua ]]; then
tmp=$(mktemp)
${luajit} -bd -- "$out" "$tmp"
mv "$tmp" "$out"
fi
return
fi
# Target is a directory
while IFS= read -r -d "" file; do
tmp=$(mktemp -u "$file.XXXX")
# Ignore invalid lua files
if ${luajit} -bd -- "$file" "$tmp"; then
mv "$tmp" "$file"
else
echo "WARNING: Ignoring byte compiling error for '$file' lua file" >&2
fi
done < <(find "$out" -type f,l -name "*.lua" -print0)
}
# Target is a directory
while IFS= read -r -d "" file; do
tmp=$(mktemp -u "$file.XXXX")
# Ignore invalid lua files
if ${luajit} -bd -- "$file" "$tmp"; then
mv "$tmp" "$file"
else
echo "WARNING: Ignoring byte compiling error for '$file' lua file" >&2
fi
done < <(find "$out" -type f,l -name "*.lua" -print0)
}
postFixupHooks+=(byteCompileLuaPostFixup)
''
);
postFixupHooks+=(byteCompileLuaPostFixup)
''
);
/*
Returns an overridden derivation with all lua files byte compiled.
@ -111,19 +133,22 @@ rec {
# Type
```
byteCompileLuaDrv :: Derivation -> Derivation
byteCompileLuaDrvWith :: pkgs -> Derivation -> Derivation
```
# Arguments
- [pkgs] A nixpkgs instance
- [drv] Input derivation
*/
byteCompileLuaDrv =
drv:
byteCompileLuaDrvWith =
pkgs: drv:
drv.overrideAttrs (
prev:
{
nativeBuildInputs = prev.nativeBuildInputs or [ ] ++ [ byteCompileLuaHook ];
nativeBuildInputs = prev.nativeBuildInputs or [ ] ++ [
(lib.nixvim.builders.byteCompileLuaHookWith pkgs)
];
}
// lib.optionalAttrs (prev ? buildCommand) {
buildCommand = ''
@ -132,4 +157,4 @@ rec {
'';
}
);
}
})