Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Radically cleanup the templates #87

Merged
merged 10 commits into from
Jul 28, 2023
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
58 changes: 0 additions & 58 deletions .github/workflows/examples.yml

This file was deleted.

37 changes: 4 additions & 33 deletions .github/workflows/templates.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,40 +8,10 @@ on:

jobs:

get-template-names:
name: Setup nix and nickel-nix
runs-on: ubuntu-latest

outputs:
templates: ${{ steps.templates.outputs.templates }}

steps:
- uses: actions/checkout@v3
with:
fetch-depth: 0

- name: Setup
uses: ./.github/actions/common-setup
with:
SECRET_GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
CACHIX_TOKEN: ${{ secrets.CACHIX_TOKEN }}

- name: List all templates
id: templates
run: |
templates=$(nix eval .#templates --apply builtins.attrNames --json)
echo "templates=$templates" >> $GITHUB_OUTPUT

enter-devshells:
name: Test devshell ${{ matrix.template }}
needs: get-template-names
name: Test devshells
runs-on: ubuntu-latest

strategy:
fail-fast: false
matrix:
template: ${{ fromJSON(needs.get-template-names.outputs.templates) }}

steps:
- uses: actions/checkout@v3
with:
Expand All @@ -53,6 +23,7 @@ jobs:
SECRET_GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
CACHIX_TOKEN: ${{ secrets.CACHIX_TOKEN }}

- name: Test devshell ${{ matrix.template }}
- name: Test devshells
run: |
nix run .#tests.templates.${{ matrix.template }}
nix run .#run-test template
nix run .#run-test example examples/c-hello-world
97 changes: 18 additions & 79 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -26,33 +26,17 @@
topiary,
} @ inputs:
{
templates = let
inherit (nixpkgs) lib;
brokenShells = ["javascript" "php" "python310"];
filteredShells = (
lib.filterAttrs
(name: value: !(builtins.elem name brokenShells))
(builtins.readDir ./templates/devshells)
);
in
lib.mapAttrs'
(
name: value:
lib.nameValuePair
(name + "-devshell")
{
path = ./templates/devshells/${name};
description = "A ${name} devshell using nickel.";
welcomeText = ''
You have created a ${name} devshell that is built using nickel!
templates.default = {
path = ./templates/default;
description = "A devshell using nickel.";
welcomeText = ''
You have created a devshell that is built using nickel!

First run `nix run .#regenerate-lockfile` to fill `nickel.lock.ncl` with proper references.
First run `nix run .#regenerate-lockfile` to fill `nickel.lock.ncl` with proper references.

Then run `nix develop` to enter the dev shell.
'';
}
)
filteredShells;
Then run `nix develop` to enter the dev shell.
'';
};

# Generate typical flake outputs from .ncl files in path for provided systems (default from flake-utils):
#
Expand Down Expand Up @@ -158,60 +142,15 @@
program = pkgs.lib.getExe (self.lib.${system}.buildLockFile contents);
};

apps.tests = let
thufschmitt marked this conversation as resolved.
Show resolved Hide resolved
mkTest = {
name,
script,
}: let
test = pkgs.writeShellApplication {
inherit name;
text = ''
set -xeuo pipefail
WORKDIR=$(mktemp -d)
function clean() {
rm -rf "''${WORKDIR}"
}
trap clean EXIT
cd "''${WORKDIR}"

${script}
'';
};
in {
type = "app";
program = "${pkgs.lib.getExe test}";
};
in {
templates = pkgs.lib.flip pkgs.lib.mapAttrs self.templates (
name: _:
mkTest {
name = "test devshell ${name}";
script = ''
nix flake new --template path:${self.outPath}#${name} example --accept-flake-config

pushd ./example
# We test against the local version of `nickel-nix`, not the one in main (hence the --override-input).
nix flake lock --override-input nickel-nix path:${self.outPath} --accept-flake-config
nix run .#regenerate-lockfile --accept-flake-config
nix develop --accept-flake-config --print-build-logs < /dev/null
popd
'';
}
);
examples = pkgs.lib.flip pkgs.lib.mapAttrs (builtins.readDir ./examples) (
name: _:
mkTest {
name = "test ${name}";
script = ''
cp -r ${self.outPath}/examples/${name} ./${name}
chmod -R u+w ./${name}
cd ./${name}
nix flake lock --override-input nickel-nix path:${self.outPath}
nix run .#regenerate-lockfile --accept-flake-config
nix build --accept-flake-config
'';
}
);
apps.run-test =
let testScript = pkgs.writeShellApplication {
name = "test-templates";
runtimeInputs = [ inputs.nickel.packages."${system}".nickel-lang-cli ];
text = builtins.readFile ./run-test.sh;
}; in
{
type = "app";
program = pkgs.lib.getExe testScript;
};

devShells.default = pkgs.mkShell {
Expand Down
9 changes: 1 addition & 8 deletions lib/builders.ncl
Original file line number Diff line number Diff line change
Expand Up @@ -166,15 +166,11 @@ in
& {
inputs_spec = {
php = {},
intelephense = {
path = "nodePackages"."intelephense",
},
},
inputs,

output.packages = {
php = inputs.php,
intelephense = inputs.intelephense,
},
},

Expand All @@ -193,7 +189,6 @@ in
},
},

# nodePackages_latest.typescript-language-server is marked broken in nixpkgs
JavascriptShell =
BashShell
& {
Expand Down Expand Up @@ -239,8 +234,6 @@ in
},
},

# broken: PyLTI-0.7.0 not supported for interpreter python3.10
# works with a regular nix shell though
Python310Shell =
BashShell
& {
Expand Down Expand Up @@ -276,7 +269,7 @@ in
HaskellStackShell =
BashShell
& {
ghcVersion, # User-defined. To keep in sync with the one used by stack
ghcVersion | default = "927", # User-defined. To keep in sync with the one used by stack
inputs_spec = {
stack = {},
ormolu = {},
Expand Down
83 changes: 83 additions & 0 deletions run-test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
#!/usr/bin/env bash

set -xeuo pipefail

usage() {
cat <<EOF
Usage:

$0 template <shellName> -- test instantiating the template using the given shell
$0 example <examplePath> -- Try running the example at <examplePath>
EOF
exit 1
}

pushd_temp () {
WORKDIR=$(mktemp -d)
function clean() {
rm -rf "${WORKDIR}"
}
trap clean EXIT
pushd "${WORKDIR}"
}

prepare_shell() {
# We test against the local version of `nickel-nix`, not the one in main (hence the --override-input).
nix flake lock --override-input nickel-nix "path:$PROJECT_ROOT" --accept-flake-config
nix run .#regenerate-lockfile --accept-flake-config
}

# Note: running in a subshell (hence the parens and not braces around the function body) so that the trap-based cleanup happens whenever we exit
test_one_template () (
target="$1"
pushd_temp

nix flake new --template "path:$PROJECT_ROOT" example --accept-flake-config

pushd ./example
sed -i "s/BashShell/$target/" dev-shell.ncl
prepare_shell
nix develop --accept-flake-config --print-build-logs < /dev/null
popd
popd
clean
)

test_template () {
if [[ -n ${1+x} ]]; then
test_one_template "$1"
else
all_targets=$(nickel export --format raw <<<'std.record.fields ((import "lib/nix.ncl").builders) |> std.string.join " "')
for target in $all_targets; do
if [[ "$target" == NickelPkg ]] || [[ "$target" == "NixpkgsPkg" ]]; then
continue
fi
test_one_template "$target"
done
fi
}

test_example () (
examplePath=$(realpath "$1")
pushd_temp
cp -r "$examplePath" ./example
pushd ./example
prepare_shell
nix build --print-build-logs
popd
popd
)

PROJECT_ROOT=$PWD

if [[ -z ${1+x} ]]; then
usage
elif [[ $1 == "template" ]]; then
shift
test_template "$@"
elif [[ $1 == "example" ]]; then
shift
test_example "$@"
else
usage
fi
8 changes: 8 additions & 0 deletions templates/default/dev-shell.ncl
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
let nixel = (import "./nickel.lock.ncl").nickel-nix in

{
output = {
name = "nickel-shell",
}
}
& nixel.builders.BashShell
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
{
description = "A basic flake with a shell";

inputs.nixpkgs.url = "nixpkgs/nixos-unstable";
inputs.nickel-nix.url = "github:nickel-lang/nickel-nix";

Expand Down
8 changes: 0 additions & 8 deletions templates/devshells/c/dev-shell.ncl

This file was deleted.

14 changes: 0 additions & 14 deletions templates/devshells/c/flake.nix

This file was deleted.

8 changes: 0 additions & 8 deletions templates/devshells/clojure/dev-shell.ncl

This file was deleted.

Loading