Skip to content

Commit

Permalink
Merge pull request #1256 from rawkode/scripts/shebangs
Browse files Browse the repository at this point in the history
feat(scripts): allow custom interpreters
  • Loading branch information
domenkozar authored Jun 8, 2024
2 parents 866ecad + ed2c68d commit 42671f2
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 17 deletions.
28 changes: 25 additions & 3 deletions examples/scripts/devenv.nix
Original file line number Diff line number Diff line change
@@ -1,20 +1,42 @@
{ config, lib, pkgs, ... }:
{ config
, lib
, pkgs
, ...
}:

{
packages = [ pkgs.curl pkgs.jq ];
packages = [
pkgs.curl
pkgs.jq
];

scripts.silly-example.exec = ''curl "https://httpbin.org/get?$1" | jq .args'';
scripts.silly-example.description = "curls httpbin with provided arg";

scripts.serious-example.exec = ''${pkgs.cowsay}/bin/cowsay "$*"'';
scripts.serious-example.description = ''echoes args in a very serious manner'';

# Example with custom package
scripts.python-hello.exec = ''print("Hello, world!")'';
scripts.python-hello.package = pkgs.python311;

# Example when package and binary are different
scripts.nushell-greet.exec = ''
def greet [name] {
["hello" $name]
}
greet "world"
'';
scripts.nushell-greet.package = pkgs.nushell;
scripts.nushell-greet.binary = "nu";

enterShell = ''
echo
echo 🦾 Helper scripts you can run to make your development richer:
echo 🦾
${pkgs.gnused}/bin/sed -e 's| |••|g' -e 's|=| |' <<EOF | ${pkgs.util-linuxMinimal}/bin/column -t | ${pkgs.gnused}/bin/sed -e 's|^|🦾 |' -e 's|••| |g'
${lib.generators.toKeyValue {} (lib.mapAttrs (name: value: value.description) config.scripts)}
${lib.generators.toKeyValue { } (lib.mapAttrs (name: value: value.description) config.scripts)}
EOF
echo
'';
Expand Down
54 changes: 40 additions & 14 deletions src/modules/scripts.nix
Original file line number Diff line number Diff line change
@@ -1,22 +1,48 @@
{ pkgs, lib, config, ... }:
{ lib
, config
, pkgs
, ...
}:

let
types = lib.types;
scriptType = types.submodule ({ config, ... }: {
options = {
exec = lib.mkOption {
type = types.str;
description = "Bash code to execute when the script is run.";
scriptType = types.submodule (
{ config, ... }:
{
options = {
exec = lib.mkOption {
type = types.str;
description = "Shell code to execute when the script is run.";
};
package = lib.mkOption {
type = types.package;
description = "The package to use to run the script.";
default = pkgs.bash;
};
binary = lib.mkOption {
type = types.str;
description = "Override the binary name if it doesn't match package name";
default = config.package.pname;
};
description = lib.mkOption {
type = types.str;
description = "Description of the script.";
default = "";
};
};
description = lib.mkOption {
type = types.str;
description = "Description of the script.";
default = "";
};
};
});
}
);

# lib.hiPrioSet: prioritize scripts over plain packages
toPackage = name: script: lib.hiPrioSet (pkgs.writeShellScriptBin name script.exec);
toPackage =
name: script:
lib.hiPrioSet (
pkgs.writeScriptBin name ''
#!${pkgs.lib.getBin script.package}/bin/${script.binary}
${script.exec}
''

);
in
{
options = {
Expand Down

0 comments on commit 42671f2

Please sign in to comment.