Skip to content
This repository has been archived by the owner on Nov 7, 2024. It is now read-only.

Implement nuenv.writeShellApplication #27

Merged
merged 2 commits into from
Nov 7, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
31 changes: 31 additions & 0 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@
# Provide helper function
prev.writeTextFile;

writeShellApplication = final.callPackage ./lib/writeShellApplication.nix { };

# TODO: mkShell
};
};
Expand Down Expand Up @@ -98,6 +100,35 @@
# function to use for writing out script (example: pkgs.writeTextFile)
writeTextFile:
internalLib.mkNushellScript nushellPkg writeTextFile;

/*
mkNushellScriptApplication creates a nushell script builder

Type:
[string] -> [string] -> package

Example:
let mkNushellScript = mkNushellScript
pkgs.nushell
pkgs.writeTextFile;
let outScript = mkNushellScript
"repair-infra.nu"
''
print -e "(ansi red)fixing infrastructure(ansi reset)"
print "dont_crash_anymore=true" | save -a server_config.toml
'';
*/
mkNushellScriptApplication =
# nushell package to use for script shebang/execution
nushellPkg:
# function to use for writing out script (example: pkgs.writeTextFile)
writeTextFile:
# nixpkgs helper functions, e.g. pkgs.lib
lib:
import ./lib/writeShellApplication.nix {
inherit writeTextFile lib;
nushell = nushellPkg;
};
};

devShells = forAllSystems ({ pkgs, system }: {
Expand Down
93 changes: 93 additions & 0 deletions lib/writeShellApplication.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
# An analogue to writeShellApplication but for Nushell rather than Bash scripts.
{ lib
, nushell
, writeTextFile
}:

let
# It might be nicer to write a nix function that translates nix expressions directly to nushell
# expressions. But since nix and nu both understand json, using that as an intermediary format is
# way easier.
toNu = v: "(\"${lib.escape ["\"" "\\"] (builtins.toJSON v)}\" | from json)";

makeBinPathArray = pkgs:
let
binOutputs = builtins.filter (x: x != null) (map (pkg: lib.getOutput "bin" pkg) pkgs);
in
map (output: output + "/bin") binOutputs;
in

{
/*
The name of the script to write.
Type: String
*/
name
, /*
The shell script's text, not including a shebang.
Type: String
*/
text
, /*
Inputs to add to the shell script's `$PATH` at runtime.
Type: [String|Derivation]
*/
runtimeInputs ? [ ]
, /*
Extra environment variables to set at runtime.
Type: AttrSet
*/
runtimeEnv ? null
, /*
`stdenv.mkDerivation`'s `meta` argument.
Type: AttrSet
*/
meta ? { }
, /*
The `checkPhase` to run. Defaults to `nu-check`.

The script path will be given as `$target` in the `checkPhase`.

Type: String
*/
checkPhase ? null
, /*
Extra arguments to pass to `stdenv.mkDerivation`.

:::{.caution}
Certain derivation attributes are used internally,
overriding those could cause problems.
:::

Type: AttrSet
*/
derivationArgs ? { }
,
}:
writeTextFile {
inherit name meta derivationArgs;
executable = true;
destination = "/bin/${name}";
allowSubstitutes = true;
preferLocalBuild = false;
text = ''
#!${nushell}/bin/nu
'' + lib.optionalString (runtimeEnv != null) ''

load-env ${toNu runtimeEnv}
'' + lib.optionalString (runtimeInputs != [ ]) ''

$env.PATH = ${toNu (makeBinPathArray runtimeInputs)} ++ $env.PATH
'' + ''

${text}
'';

checkPhase =
if checkPhase == null then ''
runHook preCheck
${nushell}/bin/nu --commands "nu-check --debug '$target'"
runHook postCheck
''
else checkPhase;
}
Loading