Skip to content

Commit

Permalink
Merge pull request #1268 from cachix/info-scripts
Browse files Browse the repository at this point in the history
info: print the script description and path in `devenv info`
  • Loading branch information
domenkozar authored Jun 12, 2024
2 parents a04f29f + efa5cbc commit bb177c4
Show file tree
Hide file tree
Showing 2 changed files with 112 additions and 94 deletions.
172 changes: 92 additions & 80 deletions devenv.nix
Original file line number Diff line number Diff line change
Expand Up @@ -40,93 +40,105 @@
tailwind.exec = "watchexec -e html,css,js npx tailwindcss build docs/assets/extra.css -o docs/assets/output.css";
};

scripts.devenv-test-cli.exec = ''
set -xe
set -o pipefail
scripts.devenv-test-cli = {
description = "Test devenv CLI.";
exec = ''
set -xe
set -o pipefail
pushd examples/simple
# this should fail since files already exist
devenv init && exit 1
popd
pushd examples/simple
# this should fail since files already exist
devenv init && exit 1
popd
tmp="$(mktemp -d)"
devenv init "$tmp"
pushd "$tmp"
devenv version
devenv --override-input devenv path:${config.devenv.root}?dir=src/modules test
popd
rm -rf "$tmp"
tmp="$(mktemp -d)"
devenv init "$tmp"
pushd "$tmp"
devenv version
devenv --override-input devenv path:${config.devenv.root}?dir=src/modules test
popd
rm -rf "$tmp"
# Test devenv integrated into bare Nix flake
tmp="$(mktemp -d)"
pushd "$tmp"
nix flake init --template ''${DEVENV_ROOT}#simple
nix flake update \
--override-input devenv ''${DEVENV_ROOT}
nix develop --accept-flake-config --impure --command echo nix-develop started succesfully |& tee ./console
grep -F 'nix-develop started succesfully' <./console
grep -F "$(${lib.getExe pkgs.hello})" <./console
# Test devenv integrated into bare Nix flake
tmp="$(mktemp -d)"
pushd "$tmp"
nix flake init --template ''${DEVENV_ROOT}#simple
nix flake update \
--override-input devenv ''${DEVENV_ROOT}
nix develop --accept-flake-config --impure --command echo nix-develop started succesfully |& tee ./console
grep -F 'nix-develop started succesfully' <./console
grep -F "$(${lib.getExe pkgs.hello})" <./console
# Assert that nix-develop fails in pure mode.
if nix develop --command echo nix-develop started in pure mode |& tee ./console
then
echo "nix-develop was able to start in pure mode. This is explicitly not supported at the moment."
exit 1
fi
grep -F 'devenv was not able to determine the current directory.' <./console
popd
rm -rf "$tmp"
# Assert that nix-develop fails in pure mode.
if nix develop --command echo nix-develop started in pure mode |& tee ./console
then
echo "nix-develop was able to start in pure mode. This is explicitly not supported at the moment."
exit 1
fi
grep -F 'devenv was not able to determine the current directory.' <./console
popd
rm -rf "$tmp"
# Test devenv integrated into flake-parts Nix flake
tmp="$(mktemp -d)"
pushd "$tmp"
nix flake init --template ''${DEVENV_ROOT}#flake-parts
nix flake update \
--override-input devenv ''${DEVENV_ROOT}
nix develop --accept-flake-config --override-input devenv-root "file+file://"<(printf %s "$PWD") --command echo nix-develop started succesfully |& tee ./console
grep -F 'nix-develop started succesfully' <./console
grep -F "$(${lib.getExe pkgs.hello})" <./console
# Test that a container can be built
if $(uname) == "Linux"
then
nix build --override-input devenv-root "file+file://"<(printf %s "$PWD") --accept-flake-config --show-trace .#container-processes
fi
popd
rm -rf "$tmp"
'';
scripts."devenv-generate-doc-options".exec = ''
set -e
output_file=docs/reference/options.md
options=$(nix build --impure --extra-experimental-features 'flakes nix-command' --show-trace --print-out-paths --no-link '.#devenv-docs-options')
echo "# devenv.nix options" > $output_file
echo >> $output_file
cat $options >> $output_file
# https://github.com/NixOS/nixpkgs/issues/224661
sed -i 's/\\\././g' $output_file
'';
scripts."devenv-generate-languages-example".exec = ''
cat > examples/supported-languages/devenv.nix <<EOF
{ pkgs, ... }: {
# Test devenv integrated into flake-parts Nix flake
tmp="$(mktemp -d)"
pushd "$tmp"
nix flake init --template ''${DEVENV_ROOT}#flake-parts
nix flake update \
--override-input devenv ''${DEVENV_ROOT}
nix develop --accept-flake-config --override-input devenv-root "file+file://"<(printf %s "$PWD") --command echo nix-develop started succesfully |& tee ./console
grep -F 'nix-develop started succesfully' <./console
grep -F "$(${lib.getExe pkgs.hello})" <./console
# Test that a container can be built
if $(uname) == "Linux"
then
nix build --override-input devenv-root "file+file://"<(printf %s "$PWD") --accept-flake-config --show-trace .#container-processes
fi
popd
rm -rf "$tmp"
'';
};
scripts."devenv-generate-doc-options" = {
description = "Generate option docs.";
exec = ''
set -e
output_file=docs/reference/options.md
options=$(nix build --impure --extra-experimental-features 'flakes nix-command' --show-trace --print-out-paths --no-link '.#devenv-docs-options')
echo "# devenv.nix options" > $output_file
echo >> $output_file
cat $options >> $output_file
# https://github.com/NixOS/nixpkgs/issues/224661
sed -i 's/\\\././g' $output_file
'';
};
scripts."devenv-generate-languages-example" = {
description = "Generate an example enabling every supported language.";
exec = ''
cat > examples/supported-languages/devenv.nix <<EOF
{ pkgs, ... }: {
# Enable all languages tooling!
${lib.concatStringsSep "\n " (map (lang: "languages.${lang}.enable = true;") (builtins.attrNames config.languages))}
# Enable all languages tooling!
${lib.concatStringsSep "\n " (map (lang: "languages.${lang}.enable = true;") (builtins.attrNames config.languages))}
# If you're missing a language, please contribute it by following examples of other languages <3
}
EOF
'';
scripts."devenv-generate-docs".exec = ''
cat > docs/services-all.md <<EOF
\`\`\`nix
${lib.concatStringsSep "\n " (map (lang: "services.${lang}.enable = true;") (builtins.attrNames config.services))}
\`\`\`
EOF
cat > docs/languages-all.md <<EOF
\`\`\`nix
${lib.concatStringsSep "\n " (map (lang: "languages.${lang}.enable = true;") (builtins.attrNames config.languages))}
\`\`\`
EOF
'';
# If you're missing a language, please contribute it by following examples of other languages <3
}
EOF
'';
};
scripts."devenv-generate-docs" = {
description = "Generate lists of all languages and services.";
exec = ''
cat > docs/services-all.md <<EOF
\`\`\`nix
${lib.concatStringsSep "\n " (map (lang: "services.${lang}.enable = true;") (builtins.attrNames config.services))}
\`\`\`
EOF
cat > docs/languages-all.md <<EOF
\`\`\`nix
${lib.concatStringsSep "\n " (map (lang: "languages.${lang}.enable = true;") (builtins.attrNames config.languages))}
\`\`\`
EOF
'';
};

pre-commit.hooks = {
nixpkgs-fmt.enable = true;
Expand Down
34 changes: 20 additions & 14 deletions src/modules/scripts.nix
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
let
types = lib.types;
scriptType = types.submodule (
{ config, ... }:
{ config, name, ... }:
{
options = {
exec = lib.mkOption {
Expand All @@ -29,20 +29,27 @@ let
description = "Description of the script.";
default = "";
};
scriptPackage = lib.mkOption {
internal = true;
type = types.package;
};
};

config.scriptPackage =
lib.hiPrioSet (
pkgs.writeScriptBin name ''
#!${pkgs.lib.getBin config.package}/bin/${config.binary}
${config.exec}
''
);
}
);

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

);
renderInfoSection = name: script:
''
${name}${lib.optionalString (script.description != "") ": ${script.description}"}
${script.scriptPackage}
'';
in
{
options = {
Expand All @@ -54,9 +61,8 @@ in
};

config = {
packages = lib.mapAttrsToList toPackage config.scripts;
packages = lib.mapAttrsToList (_: script: script.scriptPackage) config.scripts;

# TODO: show scripts path
infoSections."scripts" = lib.mapAttrsToList (name: script: name) config.scripts;
infoSections."scripts" = lib.mapAttrsToList renderInfoSection config.scripts;
};
}

0 comments on commit bb177c4

Please sign in to comment.