From 2e7982a79e627f29fdfb09fcd03c9a3b0e2ab694 Mon Sep 17 00:00:00 2001 From: Chuck Grindel Date: Wed, 6 Dec 2023 09:31:42 -0700 Subject: [PATCH 01/63] Initial files --- action.yaml | 20 ++++++++++++++++++++ tools/run_nix_shell | 38 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+) create mode 100644 action.yaml create mode 100755 tools/run_nix_shell diff --git a/action.yaml b/action.yaml new file mode 100644 index 0000000..b74eb87 --- /dev/null +++ b/action.yaml @@ -0,0 +1,20 @@ +name: Run nix-shell scripts +description: Executes shell scripts using nix-shell. + +inputs: + run: + type: string + required: true + description: The path to a file that contains shell code or the actual shell code. + ghc_version: + type: string + description: The version of the GHC compiler to use. + +runs: + using: composite + steps: + - shell: bash + env: + RNS_GHC_VERSION: ${{ inputs.ghc_version }} + RNS_RUN: ${{ inputs.run }} + run: ${GITHUB_ACTION_PATH}/run_nix_shell "${RNS_RUN}" diff --git a/tools/run_nix_shell b/tools/run_nix_shell new file mode 100755 index 0000000..c28f811 --- /dev/null +++ b/tools/run_nix_shell @@ -0,0 +1,38 @@ +#!/usr/bin/env bash + +set -o errexit -o nounset -o pipefail + +fail() { + echo >&2 "$@" + exit 1 +} + +ghc_version="${RNS_GHC_VERSION:-}" + +while (("$#")); do + case "${1}" in + "--ghc-version") + ghc_version="${2}" + shift 2 + ;; + *) + if [[ -z "${script:-}" ]]; then + script="${1}" + shift 1 + else + fail "Unexpected argument:" "${1}" + fi + ;; + esac +done + +if [[ -z "${script:-}" ]]; then + fail "A script for a path to a file must be provided." +fi + +cmd=( nix-shell --arg docTools false --pure ) +if [[ -n "${ghc_version:-}" ]]; then + cmd+=( --argstr ghcVersion "${ghc_version}" ) +fi +cmd+=( --run "${script}" ) +"${cmd[@]}" From fb288049bcac901758b7b80c1ca70f4f452cdf8d Mon Sep 17 00:00:00 2001 From: Chuck Grindel Date: Wed, 6 Dec 2023 09:45:00 -0700 Subject: [PATCH 02/63] Reworked to accept --arg* --- MODULE.bazel | 10 ++++++++++ WORKSPACE | 1 + WORKSPACE.bzlmod | 2 ++ action.yaml | 7 +++---- tools/run_nix_shell | 24 ++++++++++++++++++------ 5 files changed, 34 insertions(+), 10 deletions(-) create mode 100644 MODULE.bazel create mode 100644 WORKSPACE create mode 100644 WORKSPACE.bzlmod diff --git a/MODULE.bazel b/MODULE.bazel new file mode 100644 index 0000000..1ba8a64 --- /dev/null +++ b/MODULE.bazel @@ -0,0 +1,10 @@ +module( + name = "run_nix_shell", + version = "0.0.0", +) + +bazel_dep( + name = "cgrindel_bazel_starlib", + version = "0.18.1", + dev_dependency = True, +) diff --git a/WORKSPACE b/WORKSPACE new file mode 100644 index 0000000..b543b79 --- /dev/null +++ b/WORKSPACE @@ -0,0 +1 @@ +# Intentionally blank; using bzlmod diff --git a/WORKSPACE.bzlmod b/WORKSPACE.bzlmod new file mode 100644 index 0000000..5b29d40 --- /dev/null +++ b/WORKSPACE.bzlmod @@ -0,0 +1,2 @@ +# Intentionally blank +# This file exists to force bzlmod into a strict mode. diff --git a/action.yaml b/action.yaml index b74eb87..984937b 100644 --- a/action.yaml +++ b/action.yaml @@ -6,15 +6,14 @@ inputs: type: string required: true description: The path to a file that contains shell code or the actual shell code. - ghc_version: + args: type: string - description: The version of the GHC compiler to use. runs: using: composite steps: - shell: bash env: - RNS_GHC_VERSION: ${{ inputs.ghc_version }} + RNS_ARGS: ${{ inputs.args }} RNS_RUN: ${{ inputs.run }} - run: ${GITHUB_ACTION_PATH}/run_nix_shell "${RNS_RUN}" + run: ${GITHUB_ACTION_PATH}/tools/run_nix_shell diff --git a/tools/run_nix_shell b/tools/run_nix_shell index c28f811..197326c 100755 --- a/tools/run_nix_shell +++ b/tools/run_nix_shell @@ -2,18 +2,28 @@ set -o errexit -o nounset -o pipefail +# NOTE: This script has been implemented so that it can be executed without Bazel. Do not pull in +# external dependencies. + fail() { echo >&2 "$@" exit 1 } +# MARK - Arguments + +script="${RNS_RUN:-}" ghc_version="${RNS_GHC_VERSION:-}" +nix_shell_opts=() while (("$#")); do case "${1}" in - "--ghc-version") - ghc_version="${2}" - shift 2 + # Supports: + # --arg docTools false + # --argstr ghcVersion "1.2.3" + "--arg*") + nix_shell_opts+=( "${1}" "${2}" "${3}" ) + shift 3 ;; *) if [[ -z "${script:-}" ]]; then @@ -30,9 +40,11 @@ if [[ -z "${script:-}" ]]; then fail "A script for a path to a file must be provided." fi -cmd=( nix-shell --arg docTools false --pure ) -if [[ -n "${ghc_version:-}" ]]; then - cmd+=( --argstr ghcVersion "${ghc_version}" ) +# MARK - Execute script + +cmd=( nix-shell --pure ) +if [[ ${#nix_shell_opts[@]} -gt 0 ]]; then + cmd+=( "${nix_shell_opts[@]}" ) fi cmd+=( --run "${script}" ) "${cmd[@]}" From 97d368806521b901cafccf7e4f3fd7db1d7d1a48 Mon Sep 17 00:00:00 2001 From: Chuck Grindel Date: Wed, 6 Dec 2023 10:14:27 -0700 Subject: [PATCH 03/63] Added failing test for run_nix_shell --- .bazeliskrc | 1 + .gitignore | 1 + action.yaml | 4 ++-- tests/tools_tests/BUILD.bazel | 11 +++++++++++ tests/tools_tests/run_nix_shell_test.sh | 24 +++++++++++++++++++++++ tools/BUILD.bazel | 5 +++++ tools/{run_nix_shell => run_nix_shell.sh} | 3 ++- 7 files changed, 46 insertions(+), 3 deletions(-) create mode 100644 .bazeliskrc create mode 100644 .gitignore create mode 100644 tests/tools_tests/BUILD.bazel create mode 100755 tests/tools_tests/run_nix_shell_test.sh create mode 100644 tools/BUILD.bazel rename tools/{run_nix_shell => run_nix_shell.sh} (96%) diff --git a/.bazeliskrc b/.bazeliskrc new file mode 100644 index 0000000..7c75a11 --- /dev/null +++ b/.bazeliskrc @@ -0,0 +1 @@ +USE_BAZEL_VERSION=7.0.0rc5 diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ac51a05 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +bazel-* diff --git a/action.yaml b/action.yaml index 984937b..d57f414 100644 --- a/action.yaml +++ b/action.yaml @@ -6,7 +6,7 @@ inputs: type: string required: true description: The path to a file that contains shell code or the actual shell code. - args: + options: type: string runs: @@ -14,6 +14,6 @@ runs: steps: - shell: bash env: - RNS_ARGS: ${{ inputs.args }} + RNS_OPTS: ${{ inputs.options }} RNS_RUN: ${{ inputs.run }} run: ${GITHUB_ACTION_PATH}/tools/run_nix_shell diff --git a/tests/tools_tests/BUILD.bazel b/tests/tools_tests/BUILD.bazel new file mode 100644 index 0000000..d6265d3 --- /dev/null +++ b/tests/tools_tests/BUILD.bazel @@ -0,0 +1,11 @@ +sh_test( + name = "run_nix_shell_test", + srcs = ["run_nix_shell_test.sh"], + data = [ + "//tools:run_nix_shell", + ], + deps = [ + "@bazel_tools//tools/bash/runfiles", + "@cgrindel_bazel_starlib//shlib/lib:assertions", + ], +) diff --git a/tests/tools_tests/run_nix_shell_test.sh b/tests/tools_tests/run_nix_shell_test.sh new file mode 100755 index 0000000..40fa578 --- /dev/null +++ b/tests/tools_tests/run_nix_shell_test.sh @@ -0,0 +1,24 @@ +#!/usr/bin/env bash + +# --- begin runfiles.bash initialization v2 --- +# Copy-pasted from the Bazel Bash runfiles library v2. +set -o nounset -o pipefail; f=bazel_tools/tools/bash/runfiles/runfiles.bash +# shellcheck disable=SC1090 +source "${RUNFILES_DIR:-/dev/null}/$f" 2>/dev/null || \ + source "$(grep -sm1 "^$f " "${RUNFILES_MANIFEST_FILE:-/dev/null}" | cut -f2- -d' ')" 2>/dev/null || \ + source "$0.runfiles/$f" 2>/dev/null || \ + source "$(grep -sm1 "^$f " "$0.runfiles_manifest" | cut -f2- -d' ')" 2>/dev/null || \ + source "$(grep -sm1 "^$f " "$0.exe.runfiles_manifest" | cut -f2- -d' ')" 2>/dev/null || \ + { echo>&2 "ERROR: cannot find $f"; exit 1; }; f=; set -o errexit +# --- end runfiles.bash initialization v2 --- + +# MARK - Locate Deps + +assertions_sh_location=cgrindel_bazel_starlib/shlib/lib/assertions.sh +assertions_sh="$(rlocation "${assertions_sh_location}")" || \ + (echo >&2 "Failed to locate ${assertions_sh_location}" && exit 1) +source "${assertions_sh}" + +# MARK - Test + +fail "IMPLEMENT ME!" diff --git a/tools/BUILD.bazel b/tools/BUILD.bazel new file mode 100644 index 0000000..6442704 --- /dev/null +++ b/tools/BUILD.bazel @@ -0,0 +1,5 @@ +sh_binary( + name = "run_nix_shell", + srcs = ["run_nix_shell.sh"], + visibility = ["//:__subpackages__"], +) diff --git a/tools/run_nix_shell b/tools/run_nix_shell.sh similarity index 96% rename from tools/run_nix_shell rename to tools/run_nix_shell.sh index 197326c..9cfe8dd 100755 --- a/tools/run_nix_shell +++ b/tools/run_nix_shell.sh @@ -13,7 +13,8 @@ fail() { # MARK - Arguments script="${RNS_RUN:-}" -ghc_version="${RNS_GHC_VERSION:-}" + +# TODO(chuck): Process RNS_OPTS nix_shell_opts=() while (("$#")); do From 1e08db331b51091513f92b38012767e6c3ed023c Mon Sep 17 00:00:00 2001 From: Chuck Grindel Date: Wed, 6 Dec 2023 12:18:46 -0700 Subject: [PATCH 04/63] Initial flake.nix and flake.lock --- flake.lock | 61 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ flake.nix | 36 ++++++++++++++++++++++++++++++++ 2 files changed, 97 insertions(+) create mode 100644 flake.lock create mode 100644 flake.nix diff --git a/flake.lock b/flake.lock new file mode 100644 index 0000000..2e8bcb2 --- /dev/null +++ b/flake.lock @@ -0,0 +1,61 @@ +{ + "nodes": { + "flake-utils": { + "inputs": { + "systems": "systems" + }, + "locked": { + "lastModified": 1701680307, + "narHash": "sha256-kAuep2h5ajznlPMD9rnQyffWG8EM/C73lejGofXvdM8=", + "owner": "numtide", + "repo": "flake-utils", + "rev": "4022d587cbbfd70fe950c1e2083a02621806a725", + "type": "github" + }, + "original": { + "owner": "numtide", + "repo": "flake-utils", + "type": "github" + } + }, + "nixpkgs": { + "locked": { + "lastModified": 1685573264, + "narHash": "sha256-Zffu01pONhs/pqH07cjlF10NnMDLok8ix5Uk4rhOnZQ=", + "owner": "NixOS", + "repo": "nixpkgs", + "rev": "380be19fbd2d9079f677978361792cb25e8a3635", + "type": "github" + }, + "original": { + "owner": "NixOS", + "ref": "nixos-22.05", + "repo": "nixpkgs", + "type": "github" + } + }, + "root": { + "inputs": { + "flake-utils": "flake-utils", + "nixpkgs": "nixpkgs" + } + }, + "systems": { + "locked": { + "lastModified": 1681028828, + "narHash": "sha256-Vy1rq5AaRuLzOxct8nz4T6wlgyUR7zLU309k9mBC768=", + "owner": "nix-systems", + "repo": "default", + "rev": "da67096a3b9bf56a91d16901293e51ba5b49a27e", + "type": "github" + }, + "original": { + "owner": "nix-systems", + "repo": "default", + "type": "github" + } + } + }, + "root": "root", + "version": 7 +} diff --git a/flake.nix b/flake.nix new file mode 100644 index 0000000..a07ceaf --- /dev/null +++ b/flake.nix @@ -0,0 +1,36 @@ +{ + inputs = { + # Track a specific tag on the nixpkgs repo. + nixpkgs.url = "github:NixOS/nixpkgs/nixos-22.05"; + + # The flake format itself is very minimal, so the use of this + # library is common. + flake-utils.url = "github:numtide/flake-utils"; + }; + + # Here we can define various kinds of "outputs": packages, tests, + # and so on, but we will only define a development shell. + + outputs = { nixpkgs, flake-utils, ... }: + + # For every platform that Nix supports, we ... + flake-utils.lib.eachDefaultSystem (system: + + # ... get the package set for this particular platform ... + let pkgs = import nixpkgs { inherit system; }; + in + { + + # ... and define a development shell for it ... + devShells.default = + + # ... with no globally-available CC toolchain ... + pkgs.mkShellNoCC { + name = "rules_nixpkgs_shell"; + + # ... which makes available the following dependencies, + # all sourced from the `pkgs` package set: + packages = with pkgs; [ bazel_5 bazel-buildtools cacert nix git ]; + }; + }); +} From 16c28d51be4d547e59c5b91fb8ec3caa51efcf27 Mon Sep 17 00:00:00 2001 From: Chuck Grindel Date: Wed, 6 Dec 2023 12:22:33 -0700 Subject: [PATCH 05/63] Add nixpkgs.nix --- nixpkgs.nix | 8 ++++++++ 1 file changed, 8 insertions(+) create mode 100644 nixpkgs.nix diff --git a/nixpkgs.nix b/nixpkgs.nix new file mode 100644 index 0000000..83d5b3c --- /dev/null +++ b/nixpkgs.nix @@ -0,0 +1,8 @@ +# Ensure that our development shell uses the same Nixpkgs version as what +# rules_nixpkgs uses in Bazel. +let + lock = builtins.fromJSON (builtins.readFile ./flake.lock); + spec = lock.nodes.nixpkgs.locked; + nixpkgs = fetchTarball "https://github.com/${spec.owner}/${spec.repo}/archive/${spec.rev}.tar.gz"; +in +import nixpkgs From 423b0fca1a9f1397292b0b3dd48c835129bdf752 Mon Sep 17 00:00:00 2001 From: Chuck Grindel Date: Wed, 6 Dec 2023 12:25:23 -0700 Subject: [PATCH 06/63] Switch to using bazelisk. --- flake.nix | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/flake.nix b/flake.nix index a07ceaf..22826f4 100644 --- a/flake.nix +++ b/flake.nix @@ -30,7 +30,8 @@ # ... which makes available the following dependencies, # all sourced from the `pkgs` package set: - packages = with pkgs; [ bazel_5 bazel-buildtools cacert nix git ]; + # packages = with pkgs; [ bazel_5 bazel-buildtools cacert nix git ]; + packages = with pkgs; [ bazelisk cacert nix git ]; }; }); } From c6ef3e77c9ba4928ecaf311b37c33c9d2afd5f67 Mon Sep 17 00:00:00 2001 From: Chuck Grindel Date: Wed, 6 Dec 2023 14:11:27 -0700 Subject: [PATCH 07/63] Add envrc --- .bazelrc | 2 ++ .envrc | 1 + .gitignore | 2 ++ 3 files changed, 5 insertions(+) create mode 100644 .bazelrc create mode 100644 .envrc diff --git a/.bazelrc b/.bazelrc new file mode 100644 index 0000000..48adbff --- /dev/null +++ b/.bazelrc @@ -0,0 +1,2 @@ +# Tell Bazel that we are running on a Nix machine +build --host_platform=@io_tweag_rules_nixpkgs//nixpkgs/platforms:host diff --git a/.envrc b/.envrc new file mode 100644 index 0000000..3550a30 --- /dev/null +++ b/.envrc @@ -0,0 +1 @@ +use flake diff --git a/.gitignore b/.gitignore index ac51a05..16313eb 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,3 @@ bazel-* + +.direnv From 57efc0b1ac45e7489387408ead88427d0a981db1 Mon Sep 17 00:00:00 2001 From: Chuck Grindel Date: Wed, 6 Dec 2023 14:14:45 -0700 Subject: [PATCH 08/63] Add rules_nixpkgs --- .bazelrc | 2 +- MODULE.bazel | 8 ++++++++ non_module_deps.bzl | 20 ++++++++++++++++++++ 3 files changed, 29 insertions(+), 1 deletion(-) create mode 100644 non_module_deps.bzl diff --git a/.bazelrc b/.bazelrc index 48adbff..5bba608 100644 --- a/.bazelrc +++ b/.bazelrc @@ -1,2 +1,2 @@ # Tell Bazel that we are running on a Nix machine -build --host_platform=@io_tweag_rules_nixpkgs//nixpkgs/platforms:host +build --host_platform=@rules_nixpkgs_core//platforms:host diff --git a/MODULE.bazel b/MODULE.bazel index 1ba8a64..dd00976 100644 --- a/MODULE.bazel +++ b/MODULE.bazel @@ -3,8 +3,16 @@ module( version = "0.0.0", ) +bazel_dep( + name = "rules_nixpkgs_core", + version = "0.10.0", +) + bazel_dep( name = "cgrindel_bazel_starlib", version = "0.18.1", dev_dependency = True, ) + +non_module_deps = use_extension("//:non_module_deps.bzl", "non_module_deps") +use_repo(non_module_deps, "nixpkgs_default") diff --git a/non_module_deps.bzl b/non_module_deps.bzl new file mode 100644 index 0000000..081600a --- /dev/null +++ b/non_module_deps.bzl @@ -0,0 +1,20 @@ +"""Load nixpkgs""" + +load( + "@rules_nixpkgs_core//:nixpkgs.bzl", + "nixpkgs_local_repository", +) + +def repositories(): + nixpkgs_local_repository( + name = "@nixpkgs", + nix_file = "//:nixpkgs.nix", + nix_file_deps = ["//:flake.lock"], + ) + +def _non_module_deps_impl(_ctx): + repositories(bzlmod = True) + +non_module_deps = module_extension( + implementation = _non_module_deps_impl, +) From 723e67960ecc610c6a6e90c4697e15919d36915b Mon Sep 17 00:00:00 2001 From: Chuck Grindel Date: Wed, 6 Dec 2023 17:03:52 -0700 Subject: [PATCH 09/63] Update nixpkgs to more recent version --- MODULE.bazel | 17 +++++++++++++++-- flake.lock | 25 +++++++++++++++++++++---- flake.nix | 9 ++++++++- non_module_deps.bzl | 20 -------------------- tests/tools_tests/BUILD.bazel | 3 +++ tests/tools_tests/run_nix_shell_test.sh | 12 ++++++++++++ 6 files changed, 59 insertions(+), 27 deletions(-) delete mode 100644 non_module_deps.bzl diff --git a/MODULE.bazel b/MODULE.bazel index dd00976..90bc6a8 100644 --- a/MODULE.bazel +++ b/MODULE.bazel @@ -14,5 +14,18 @@ bazel_dep( dev_dependency = True, ) -non_module_deps = use_extension("//:non_module_deps.bzl", "non_module_deps") -use_repo(non_module_deps, "nixpkgs_default") +nix_repo = use_extension( + "@rules_nixpkgs_core//extensions:repository.bzl", + "nix_repo", +) +nix_repo.file( + name = "file_nixpkgs", + file = "//:nixpkgs.nix", + file_deps = [ + "//:flake.lock", + ], +) +use_repo( + nix_repo, + "file_nixpkgs", +) diff --git a/flake.lock b/flake.lock index 2e8bcb2..61da95c 100644 --- a/flake.lock +++ b/flake.lock @@ -1,5 +1,21 @@ { "nodes": { + "flake-compat": { + "flake": false, + "locked": { + "lastModified": 1696426674, + "narHash": "sha256-kvjfFW7WAETZlt09AgDn1MrtKzP7t90Vf7vypd3OL1U=", + "owner": "edolstra", + "repo": "flake-compat", + "rev": "0f9255e01c2351cc7d116c072cb317785dd33b33", + "type": "github" + }, + "original": { + "owner": "edolstra", + "repo": "flake-compat", + "type": "github" + } + }, "flake-utils": { "inputs": { "systems": "systems" @@ -20,22 +36,23 @@ }, "nixpkgs": { "locked": { - "lastModified": 1685573264, - "narHash": "sha256-Zffu01pONhs/pqH07cjlF10NnMDLok8ix5Uk4rhOnZQ=", + "lastModified": 1701539137, + "narHash": "sha256-nVO/5QYpf1GwjvtpXhyxx5M3U/WN0MwBro4Lsk+9mL0=", "owner": "NixOS", "repo": "nixpkgs", - "rev": "380be19fbd2d9079f677978361792cb25e8a3635", + "rev": "933d7dc155096e7575d207be6fb7792bc9f34f6d", "type": "github" }, "original": { "owner": "NixOS", - "ref": "nixos-22.05", + "ref": "nixos-23.11", "repo": "nixpkgs", "type": "github" } }, "root": { "inputs": { + "flake-compat": "flake-compat", "flake-utils": "flake-utils", "nixpkgs": "nixpkgs" } diff --git a/flake.nix b/flake.nix index 22826f4..bb0bb40 100644 --- a/flake.nix +++ b/flake.nix @@ -1,11 +1,18 @@ { inputs = { # Track a specific tag on the nixpkgs repo. - nixpkgs.url = "github:NixOS/nixpkgs/nixos-22.05"; + nixpkgs.url = "github:NixOS/nixpkgs/nixos-23.11"; # The flake format itself is very minimal, so the use of this # library is common. flake-utils.url = "github:numtide/flake-utils"; + + # Library for working with tools that need default.nix and shell.nix. + # https://nixos.wiki/wiki/Flakes#Using_flakes_with_stable_Nix + flake-compat = { + url = "github:edolstra/flake-compat"; + flake = false; + }; }; # Here we can define various kinds of "outputs": packages, tests, diff --git a/non_module_deps.bzl b/non_module_deps.bzl deleted file mode 100644 index 081600a..0000000 --- a/non_module_deps.bzl +++ /dev/null @@ -1,20 +0,0 @@ -"""Load nixpkgs""" - -load( - "@rules_nixpkgs_core//:nixpkgs.bzl", - "nixpkgs_local_repository", -) - -def repositories(): - nixpkgs_local_repository( - name = "@nixpkgs", - nix_file = "//:nixpkgs.nix", - nix_file_deps = ["//:flake.lock"], - ) - -def _non_module_deps_impl(_ctx): - repositories(bzlmod = True) - -non_module_deps = module_extension( - implementation = _non_module_deps_impl, -) diff --git a/tests/tools_tests/BUILD.bazel b/tests/tools_tests/BUILD.bazel index d6265d3..7226854 100644 --- a/tests/tools_tests/BUILD.bazel +++ b/tests/tools_tests/BUILD.bazel @@ -4,6 +4,9 @@ sh_test( data = [ "//tools:run_nix_shell", ], + # MacOS sandbox fails this test with the following error: + # sandbox-exec: sandbox_apply: Operation not permitted + tags = ["no-sandbox"], deps = [ "@bazel_tools//tools/bash/runfiles", "@cgrindel_bazel_starlib//shlib/lib:assertions", diff --git a/tests/tools_tests/run_nix_shell_test.sh b/tests/tools_tests/run_nix_shell_test.sh index 40fa578..1022bef 100755 --- a/tests/tools_tests/run_nix_shell_test.sh +++ b/tests/tools_tests/run_nix_shell_test.sh @@ -19,6 +19,18 @@ assertions_sh="$(rlocation "${assertions_sh_location}")" || \ (echo >&2 "Failed to locate ${assertions_sh_location}" && exit 1) source "${assertions_sh}" +run_nix_shell_sh_location=run_nix_shell/tools/run_nix_shell.sh +run_nix_shell_sh="$(rlocation "${run_nix_shell_sh_location}")" || \ + (echo >&2 "Failed to locate ${run_nix_shell_sh_location}" && exit 1) + # MARK - Test +# DEBUG BEGIN +echo >&2 "*** CHUCK $(basename "${BASH_SOURCE[0]}") PWD: ${PWD}" +echo >&2 "*** CHUCK $(basename "${BASH_SOURCE[0]}") TEST_TMPDIR: ${TEST_TMPDIR}" + +# DEBUG END + +"${run_nix_shell_sh}" 'echo "Hello, World!"' + fail "IMPLEMENT ME!" From 1af9c11b74cbfd396de97ac18eeec9ccb78819c4 Mon Sep 17 00:00:00 2001 From: Chuck Grindel Date: Thu, 7 Dec 2023 12:26:52 -0700 Subject: [PATCH 10/63] The test ran successfully. --- .bazelrc | 2 ++ BUILD.bazel | 10 ++++++++++ flake.lock | 8 ++++---- flake.nix | 22 ++++++++++++++++------ tests/tools_tests/BUILD.bazel | 1 + tests/tools_tests/run_nix_shell_test.sh | 24 +++++++++++++++++++++++- 6 files changed, 56 insertions(+), 11 deletions(-) create mode 100644 BUILD.bazel diff --git a/.bazelrc b/.bazelrc index 5bba608..b52a332 100644 --- a/.bazelrc +++ b/.bazelrc @@ -1,2 +1,4 @@ +build --enable_bzlmod + # Tell Bazel that we are running on a Nix machine build --host_platform=@rules_nixpkgs_core//platforms:host diff --git a/BUILD.bazel b/BUILD.bazel new file mode 100644 index 0000000..b4297f0 --- /dev/null +++ b/BUILD.bazel @@ -0,0 +1,10 @@ +# exports_files(["flake.lock"]) + +filegroup( + name = "flake_files", + srcs = [ + "flake.lock", + "flake.nix", + ], + visibility = ["//:__subpackages__"], +) diff --git a/flake.lock b/flake.lock index 61da95c..e4ec50e 100644 --- a/flake.lock +++ b/flake.lock @@ -36,16 +36,16 @@ }, "nixpkgs": { "locked": { - "lastModified": 1701539137, - "narHash": "sha256-nVO/5QYpf1GwjvtpXhyxx5M3U/WN0MwBro4Lsk+9mL0=", + "lastModified": 1701718080, + "narHash": "sha256-6ovz0pG76dE0P170pmmZex1wWcQoeiomUZGggfH9XPs=", "owner": "NixOS", "repo": "nixpkgs", - "rev": "933d7dc155096e7575d207be6fb7792bc9f34f6d", + "rev": "2c7f3c0fb7c08a0814627611d9d7d45ab6d75335", "type": "github" }, "original": { "owner": "NixOS", - "ref": "nixos-23.11", + "ref": "nixos-unstable", "repo": "nixpkgs", "type": "github" } diff --git a/flake.nix b/flake.nix index bb0bb40..a3c1960 100644 --- a/flake.nix +++ b/flake.nix @@ -1,7 +1,8 @@ { inputs = { # Track a specific tag on the nixpkgs repo. - nixpkgs.url = "github:NixOS/nixpkgs/nixos-23.11"; + # nixpkgs.url = "github:NixOS/nixpkgs/nixos-23.11"; + nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable"; # The flake format itself is very minimal, so the use of this # library is common. @@ -29,16 +30,25 @@ { # ... and define a development shell for it ... - devShells.default = + devShells.default = with pkgs; mkShell { + # do not use Xcode on macOS + BAZEL_USE_CPP_ONLY_TOOLCHAIN = "1"; + # for nixpkgs cc wrappers, select C++ explicitly (see https://github.com/NixOS/nixpkgs/issues/150655) + BAZEL_CXXOPTS = "-x:c++"; - # ... with no globally-available CC toolchain ... - pkgs.mkShellNoCC { - name = "rules_nixpkgs_shell"; + # name = "rules_nixpkgs_shell"; + # buildInputs = lib.optional pkgs.stdenv.isDarwin darwin.cctools; + # packages = [ bazel_6 bazel-buildtools cacert gcc nix git openssh ]; + name = "run_nix_shell_shell"; # ... which makes available the following dependencies, # all sourced from the `pkgs` package set: # packages = with pkgs; [ bazel_5 bazel-buildtools cacert nix git ]; - packages = with pkgs; [ bazelisk cacert nix git ]; + # packages = with pkgs; [ bazel_6 cacert nix git ]; + # packages = with pkgs; [ bazelisk cacert nix git ]; + + buildInputs = lib.optional pkgs.stdenv.isDarwin darwin.cctools; + packages = [ bazel_6 bazel-buildtools cacert gcc nix git openssh ]; }; }); } diff --git a/tests/tools_tests/BUILD.bazel b/tests/tools_tests/BUILD.bazel index 7226854..b0a5b9a 100644 --- a/tests/tools_tests/BUILD.bazel +++ b/tests/tools_tests/BUILD.bazel @@ -2,6 +2,7 @@ sh_test( name = "run_nix_shell_test", srcs = ["run_nix_shell_test.sh"], data = [ + "//:flake_files", "//tools:run_nix_shell", ], # MacOS sandbox fails this test with the following error: diff --git a/tests/tools_tests/run_nix_shell_test.sh b/tests/tools_tests/run_nix_shell_test.sh index 1022bef..5614f2e 100755 --- a/tests/tools_tests/run_nix_shell_test.sh +++ b/tests/tools_tests/run_nix_shell_test.sh @@ -23,12 +23,34 @@ run_nix_shell_sh_location=run_nix_shell/tools/run_nix_shell.sh run_nix_shell_sh="$(rlocation "${run_nix_shell_sh_location}")" || \ (echo >&2 "Failed to locate ${run_nix_shell_sh_location}" && exit 1) +flake_lock_location=run_nix_shell/flake.lock +flake_lock="$(rlocation "${flake_lock_location}")" || \ + (echo >&2 "Failed to locate ${flake_lock_location}" && exit 1) + # MARK - Test # DEBUG BEGIN echo >&2 "*** CHUCK $(basename "${BASH_SOURCE[0]}") PWD: ${PWD}" -echo >&2 "*** CHUCK $(basename "${BASH_SOURCE[0]}") TEST_TMPDIR: ${TEST_TMPDIR}" +echo >&2 "*** CHUCK $(basename "${BASH_SOURCE[0]}") flake_lock: ${flake_lock}" +tree +# DEBUG END +cat >shell.nix <<-EOF +(import + ( + let lock = builtins.fromJSON (builtins.readFile ${flake_lock}); in + fetchTarball { + url = "https://github.com/edolstra/flake-compat/archive/\${lock.nodes.flake-compat.locked.rev}.tar.gz"; + sha256 = lock.nodes.flake-compat.locked.narHash; + } + ) + { src = ./.; } +).shellNix +EOF + +# DEBUG BEGIN +echo >&2 "*** CHUCK $(basename "${BASH_SOURCE[0]}") shell.nix:" +cat >&2 shell.nix # DEBUG END "${run_nix_shell_sh}" 'echo "Hello, World!"' From fe120df186094974065e91aacda901fa98b8415b Mon Sep 17 00:00:00 2001 From: Chuck Grindel Date: Thu, 7 Dec 2023 12:31:58 -0700 Subject: [PATCH 11/63] Clean up --- flake.nix | 31 ++++--------------------- tests/tools_tests/run_nix_shell_test.sh | 11 --------- 2 files changed, 5 insertions(+), 37 deletions(-) diff --git a/flake.nix b/flake.nix index a3c1960..5d87423 100644 --- a/flake.nix +++ b/flake.nix @@ -1,13 +1,7 @@ { inputs = { - # Track a specific tag on the nixpkgs repo. - # nixpkgs.url = "github:NixOS/nixpkgs/nixos-23.11"; nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable"; - - # The flake format itself is very minimal, so the use of this - # library is common. flake-utils.url = "github:numtide/flake-utils"; - # Library for working with tools that need default.nix and shell.nix. # https://nixos.wiki/wiki/Flakes#Using_flakes_with_stable_Nix flake-compat = { @@ -15,39 +9,24 @@ flake = false; }; }; - - # Here we can define various kinds of "outputs": packages, tests, - # and so on, but we will only define a development shell. - outputs = { nixpkgs, flake-utils, ... }: - # For every platform that Nix supports, we ... flake-utils.lib.eachDefaultSystem (system: - # ... get the package set for this particular platform ... let pkgs = import nixpkgs { inherit system; }; - in - { - + in { # ... and define a development shell for it ... devShells.default = with pkgs; mkShell { + # BEGIN Config to build Bazel 6 # do not use Xcode on macOS BAZEL_USE_CPP_ONLY_TOOLCHAIN = "1"; # for nixpkgs cc wrappers, select C++ explicitly (see https://github.com/NixOS/nixpkgs/issues/150655) BAZEL_CXXOPTS = "-x:c++"; - - # name = "rules_nixpkgs_shell"; - # buildInputs = lib.optional pkgs.stdenv.isDarwin darwin.cctools; - # packages = [ bazel_6 bazel-buildtools cacert gcc nix git openssh ]; + buildInputs = lib.optional pkgs.stdenv.isDarwin darwin.cctools; + # END Config to build Bazel 6 + # Name for the shell name = "run_nix_shell_shell"; - # ... which makes available the following dependencies, - # all sourced from the `pkgs` package set: - # packages = with pkgs; [ bazel_5 bazel-buildtools cacert nix git ]; - # packages = with pkgs; [ bazel_6 cacert nix git ]; - # packages = with pkgs; [ bazelisk cacert nix git ]; - - buildInputs = lib.optional pkgs.stdenv.isDarwin darwin.cctools; packages = [ bazel_6 bazel-buildtools cacert gcc nix git openssh ]; }; }); diff --git a/tests/tools_tests/run_nix_shell_test.sh b/tests/tools_tests/run_nix_shell_test.sh index 5614f2e..716ada1 100755 --- a/tests/tools_tests/run_nix_shell_test.sh +++ b/tests/tools_tests/run_nix_shell_test.sh @@ -29,12 +29,6 @@ flake_lock="$(rlocation "${flake_lock_location}")" || \ # MARK - Test -# DEBUG BEGIN -echo >&2 "*** CHUCK $(basename "${BASH_SOURCE[0]}") PWD: ${PWD}" -echo >&2 "*** CHUCK $(basename "${BASH_SOURCE[0]}") flake_lock: ${flake_lock}" -tree -# DEBUG END - cat >shell.nix <<-EOF (import ( @@ -48,11 +42,6 @@ cat >shell.nix <<-EOF ).shellNix EOF -# DEBUG BEGIN -echo >&2 "*** CHUCK $(basename "${BASH_SOURCE[0]}") shell.nix:" -cat >&2 shell.nix -# DEBUG END - "${run_nix_shell_sh}" 'echo "Hello, World!"' fail "IMPLEMENT ME!" From 69bd851eaf3932bf3847f81ae26ee6a11a7a4f17 Mon Sep 17 00:00:00 2001 From: Chuck Grindel Date: Thu, 7 Dec 2023 12:49:37 -0700 Subject: [PATCH 12/63] Initial tests are green --- tests/tools_tests/run_nix_shell_test.sh | 29 ++++++++++++++++++++++--- tools/run_nix_shell.sh | 7 ++++++ 2 files changed, 33 insertions(+), 3 deletions(-) diff --git a/tests/tools_tests/run_nix_shell_test.sh b/tests/tools_tests/run_nix_shell_test.sh index 716ada1..d6d87a8 100755 --- a/tests/tools_tests/run_nix_shell_test.sh +++ b/tests/tools_tests/run_nix_shell_test.sh @@ -27,7 +27,7 @@ flake_lock_location=run_nix_shell/flake.lock flake_lock="$(rlocation "${flake_lock_location}")" || \ (echo >&2 "Failed to locate ${flake_lock_location}" && exit 1) -# MARK - Test +# MARK - Setup cat >shell.nix <<-EOF (import @@ -42,6 +42,29 @@ cat >shell.nix <<-EOF ).shellNix EOF -"${run_nix_shell_sh}" 'echo "Hello, World!"' +# MARK - Test + +assert_msg="simple script" +output="$( "${run_nix_shell_sh}" 'echo "Hello, World!"' )" +assert_equal "Hello, World!" "${output}" "${assert_msg}" -fail "IMPLEMENT ME!" +assert_msg="multi-line script" +output="$( +"${run_nix_shell_sh}" ' +echo "Hello, World!" +echo "Second Line" +' +)" +assert_match "Hello, World" "${output}" "${assert_msg}" +assert_match "Second Line" "${output}" "${assert_msg}" + +assert_msg="path to script file" +custom_script_path="./custom_script.sh" +cat >"${custom_script_path}" <<-EOF +#!/usr/bin/env bash +set -o errexit -o nounset -o pipefail +echo "Hello from custom script" +EOF +chmod +x "${custom_script_path}" +output="$( "${run_nix_shell_sh}" "${custom_script_path}" )" +assert_equal "Hello from custom script" "${output}" "${assert_msg}" diff --git a/tools/run_nix_shell.sh b/tools/run_nix_shell.sh index 9cfe8dd..7d70965 100755 --- a/tools/run_nix_shell.sh +++ b/tools/run_nix_shell.sh @@ -41,6 +41,13 @@ if [[ -z "${script:-}" ]]; then fail "A script for a path to a file must be provided." fi +# DEBUG BEGIN +echo >&2 "*** CHUCK $(basename "${BASH_SOURCE[0]}") =================" +echo >&2 "*** CHUCK $(basename "${BASH_SOURCE[0]}") script: ${script}" +tree >&2 +set -x +# DEBUG END + # MARK - Execute script cmd=( nix-shell --pure ) From f6c0a8bb632c00cc96648a2d922d9f6c88f91f8a Mon Sep 17 00:00:00 2001 From: Chuck Grindel Date: Thu, 7 Dec 2023 14:43:51 -0700 Subject: [PATCH 13/63] Processing args from command line --- tests/tools_tests/run_nix_shell_test.sh | 38 ++++++++++++++++++++----- tools/run_nix_shell.sh | 9 +----- 2 files changed, 32 insertions(+), 15 deletions(-) diff --git a/tests/tools_tests/run_nix_shell_test.sh b/tests/tools_tests/run_nix_shell_test.sh index d6d87a8..442927a 100755 --- a/tests/tools_tests/run_nix_shell_test.sh +++ b/tests/tools_tests/run_nix_shell_test.sh @@ -30,16 +30,23 @@ flake_lock="$(rlocation "${flake_lock_location}")" || \ # MARK - Setup cat >shell.nix <<-EOF -(import - ( +{ + pkgs ? import ( let lock = builtins.fromJSON (builtins.readFile ${flake_lock}); in fetchTarball { - url = "https://github.com/edolstra/flake-compat/archive/\${lock.nodes.flake-compat.locked.rev}.tar.gz"; - sha256 = lock.nodes.flake-compat.locked.narHash; + url = "https://github.com/NixOS/nixpkgs/archive/\${lock.nodes.nixpkgs.locked.rev}.tar.gz"; + sha256 = lock.nodes.nixpkgs.locked.narHash; } - ) - { src = ./.; } -).shellNix + ) {}, + customVarBool ? false, + customVarStr ? "default", +}: +with pkgs; +mkShell { + CUSTOM_VAR_BOOL = if customVarBool then "true" else "false"; + CUSTOM_VAR_STR = customVarStr; + packages = [ nix ]; +} EOF # MARK - Test @@ -68,3 +75,20 @@ EOF chmod +x "${custom_script_path}" output="$( "${run_nix_shell_sh}" "${custom_script_path}" )" assert_equal "Hello from custom script" "${output}" "${assert_msg}" + +assert_msg="default value for CUSTOM_VAR_BOOl" +output="$( "${run_nix_shell_sh}" 'echo "${CUSTOM_VAR_BOOL}"' )" +assert_equal "false" "${output}" "${assert_msg}" + +assert_msg="default value for CUSTOM_VAR_STR" +output="$( "${run_nix_shell_sh}" 'echo "${CUSTOM_VAR_STR}"' )" +assert_equal "default" "${output}" "${assert_msg}" + +assert_msg="custom value for CUSTOM_VAR_BOOL" +output="$( "${run_nix_shell_sh}" --arg customVarBool true 'echo "${CUSTOM_VAR_BOOL}"' )" +assert_equal "true" "${output}" "${assert_msg}" + +assert_msg="custom value for CUSTOM_VAR_STR" +expected="This is a custom value." +output="$( "${run_nix_shell_sh}" --argstr customVarStr "${expected}" 'echo "${CUSTOM_VAR_STR}"' )" +assert_equal "${expected}" "${output}" "${assert_msg}" diff --git a/tools/run_nix_shell.sh b/tools/run_nix_shell.sh index 7d70965..6189c56 100755 --- a/tools/run_nix_shell.sh +++ b/tools/run_nix_shell.sh @@ -22,7 +22,7 @@ while (("$#")); do # Supports: # --arg docTools false # --argstr ghcVersion "1.2.3" - "--arg*") + --arg*) nix_shell_opts+=( "${1}" "${2}" "${3}" ) shift 3 ;; @@ -41,13 +41,6 @@ if [[ -z "${script:-}" ]]; then fail "A script for a path to a file must be provided." fi -# DEBUG BEGIN -echo >&2 "*** CHUCK $(basename "${BASH_SOURCE[0]}") =================" -echo >&2 "*** CHUCK $(basename "${BASH_SOURCE[0]}") script: ${script}" -tree >&2 -set -x -# DEBUG END - # MARK - Execute script cmd=( nix-shell --pure ) From fb5c1d4f1a13c5669f8f60e75916918c6e16097a Mon Sep 17 00:00:00 2001 From: Chuck Grindel Date: Thu, 7 Dec 2023 16:55:38 -0700 Subject: [PATCH 14/63] Clean up --- tests/tools_tests/run_nix_shell_test.sh | 12 ++++++++++-- tools/run_nix_shell.sh | 15 +++++++++++++-- 2 files changed, 23 insertions(+), 4 deletions(-) diff --git a/tests/tools_tests/run_nix_shell_test.sh b/tests/tools_tests/run_nix_shell_test.sh index 442927a..f8c8727 100755 --- a/tests/tools_tests/run_nix_shell_test.sh +++ b/tests/tools_tests/run_nix_shell_test.sh @@ -84,11 +84,19 @@ assert_msg="default value for CUSTOM_VAR_STR" output="$( "${run_nix_shell_sh}" 'echo "${CUSTOM_VAR_STR}"' )" assert_equal "default" "${output}" "${assert_msg}" -assert_msg="custom value for CUSTOM_VAR_BOOL" +assert_msg="custom value for CUSTOM_VAR_BOOL via command-line arg" output="$( "${run_nix_shell_sh}" --arg customVarBool true 'echo "${CUSTOM_VAR_BOOL}"' )" assert_equal "true" "${output}" "${assert_msg}" -assert_msg="custom value for CUSTOM_VAR_STR" +assert_msg="custom value for CUSTOM_VAR_STR via command-line arg" expected="This is a custom value." output="$( "${run_nix_shell_sh}" --argstr customVarStr "${expected}" 'echo "${CUSTOM_VAR_STR}"' )" assert_equal "${expected}" "${output}" "${assert_msg}" + +assert_msg="custom value for CUSTOM_VAR_BOOL via RNS_OPT" +output="$( +RNS_OPTS='--arg customVarBool true --argstr customVarStr "This is a custom value."' \ + "${run_nix_shell_sh}" 'echo "${CUSTOM_VAR_BOOL}"; echo "${CUSTOM_VAR_STR}"' +)" +assert_match "true" "${output}" "${assert_msg}" +assert_match "This is a custom value" "${output}" "${assert_msg}" diff --git a/tools/run_nix_shell.sh b/tools/run_nix_shell.sh index 6189c56..849ff4e 100755 --- a/tools/run_nix_shell.sh +++ b/tools/run_nix_shell.sh @@ -14,8 +14,6 @@ fail() { script="${RNS_RUN:-}" -# TODO(chuck): Process RNS_OPTS - nix_shell_opts=() while (("$#")); do case "${1}" in @@ -37,6 +35,19 @@ while (("$#")); do esac done +# Look for any options passed via the RNS_OPTS environment variable. +# Add them to the nix_shell_opts. +if [[ -n "${RNS_OPTS:-}" ]]; then + # Parse the RNS_OPTS string. + while IFS=$'\0' read -r -d '' arg; do + more_nix_shell_opts+=( "${arg}" ) + done < <(xargs printf '%s\0' <<<"${RNS_OPTS}") + # Add to the nix_shell_opts if we found any + if [[ ${#more_nix_shell_opts[@]} ]]; then + nix_shell_opts+=( "${more_nix_shell_opts[@]}" ) + fi +fi + if [[ -z "${script:-}" ]]; then fail "A script for a path to a file must be provided." fi From 7c5f985f381f580c83788895827c84d2ad1cf0de Mon Sep 17 00:00:00 2001 From: Chuck Grindel Date: Thu, 7 Dec 2023 16:58:32 -0700 Subject: [PATCH 15/63] Remove bazeliskrc --- .bazeliskrc | 1 - 1 file changed, 1 deletion(-) delete mode 100644 .bazeliskrc diff --git a/.bazeliskrc b/.bazeliskrc deleted file mode 100644 index 7c75a11..0000000 --- a/.bazeliskrc +++ /dev/null @@ -1 +0,0 @@ -USE_BAZEL_VERSION=7.0.0rc5 From eba941809bf70ed7f2bd23561f99d60bcbfda282 Mon Sep 17 00:00:00 2001 From: Chuck Grindel Date: Thu, 7 Dec 2023 17:06:12 -0700 Subject: [PATCH 16/63] Update bazelrc to load auth and local --- .bazelrc | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/.bazelrc b/.bazelrc index b52a332..ff367b1 100644 --- a/.bazelrc +++ b/.bazelrc @@ -2,3 +2,11 @@ build --enable_bzlmod # Tell Bazel that we are running on a Nix machine build --host_platform=@rules_nixpkgs_core//platforms:host + +# Remote Cache Authentication +# --------------------------- +try-import %workspace%/.bazelrc.auth + +# User Configuration +# ------------------ +try-import %workspace%/.bazelrc.local From c70a672e550aeb762f23f34a2f2a1e4974935678 Mon Sep 17 00:00:00 2001 From: Chuck Grindel Date: Thu, 7 Dec 2023 17:07:41 -0700 Subject: [PATCH 17/63] Remove rules_nixpkgs. Not needed. --- .bazelrc | 3 --- MODULE.bazel | 16 ---------------- 2 files changed, 19 deletions(-) diff --git a/.bazelrc b/.bazelrc index ff367b1..ebd91e5 100644 --- a/.bazelrc +++ b/.bazelrc @@ -1,8 +1,5 @@ build --enable_bzlmod -# Tell Bazel that we are running on a Nix machine -build --host_platform=@rules_nixpkgs_core//platforms:host - # Remote Cache Authentication # --------------------------- try-import %workspace%/.bazelrc.auth diff --git a/MODULE.bazel b/MODULE.bazel index 90bc6a8..db209bc 100644 --- a/MODULE.bazel +++ b/MODULE.bazel @@ -13,19 +13,3 @@ bazel_dep( version = "0.18.1", dev_dependency = True, ) - -nix_repo = use_extension( - "@rules_nixpkgs_core//extensions:repository.bzl", - "nix_repo", -) -nix_repo.file( - name = "file_nixpkgs", - file = "//:nixpkgs.nix", - file_deps = [ - "//:flake.lock", - ], -) -use_repo( - nix_repo, - "file_nixpkgs", -) From 335c53a0fa16ae03667d6f88de91fba658e522e6 Mon Sep 17 00:00:00 2001 From: Chuck Grindel Date: Thu, 7 Dec 2023 17:24:28 -0700 Subject: [PATCH 18/63] First pass at CI --- .github/workflows/ci.yaml | 49 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 .github/workflows/ci.yaml diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml new file mode 100644 index 0000000..2cdbcad --- /dev/null +++ b/.github/workflows/ci.yaml @@ -0,0 +1,49 @@ +name: Continuous integration +on: + push: + branches: main + pull_request: + branches: main + workflow_dispatch: # allows manual triggering + schedule: + - cron: '1 11 * * *' + +concurrency: + group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }} + cancel-in-progress: ${{ github.ref != 'refs/heads/master' }} + +jobs: + unit_tests: + name: Build & Test - Nixpkgs + strategy: + fail-fast: false + matrix: + os: [ubuntu-latest, macos-11] + runs-on: ${{ matrix.os }} + steps: + - name: Checkout the repository + uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4 + - uses: cachix/install-nix-action@v24 + with: + nix_path: nixpkgs=./default.nix + extra_nix_config: | + trusted-public-keys = cache.nixos.org-1:6NCHdD59X431o0gWypbMrAURkbJ16ZPMQFGspcDShjY= hydra.iohk.io:f/Ea+s+dFdN+3Y/G+FDgSq+a5NEWhJGzdjvKNGv0/EQ= + extra-substituters = https://cache.iog.io + - uses: tweag/configure-bazel-remote-cache-auth@v0 + with: + buildbuddy_api_key: ${{ secrets.BUILDBUDDY_API_KEY }} + bazelrc_path: .bazelrc.auth + - name: Execute Bazel tests + shell: bash + run: | + bazel test //... + + all_ci_tests: + runs-on: ubuntu-latest + needs: + - unit_tests + if: ${{ always() }} + steps: + - uses: cgrindel/gha_join_jobs@794a2d117251f22607f1aab937d3fd3eaaf9a2f5 # v1 + with: + github_token: ${{ secrets.GITHUB_TOKEN }} From c52bdebaccfcb8aa68c425265a861c49be6612fd Mon Sep 17 00:00:00 2001 From: Chuck Grindel Date: Thu, 7 Dec 2023 17:27:05 -0700 Subject: [PATCH 19/63] Add default.nix. --- default.nix | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 default.nix diff --git a/default.nix b/default.nix new file mode 100644 index 0000000..fecd44a --- /dev/null +++ b/default.nix @@ -0,0 +1,11 @@ +# This exists so that we can set up the Nix environment in CI using cachix/install-nix-action. +(import + ( + let lock = builtins.fromJSON (builtins.readFile ./flake.lock); in + fetchTarball { + url = lock.nodes.flake-compat.locked.url or "https://github.com/edolstra/flake-compat/archive/${lock.nodes.flake-compat.locked.rev}.tar.gz"; + sha256 = lock.nodes.flake-compat.locked.narHash; + } + ) + { src = ./.; } +).defaultNix From 0bfe1d27c48225abddcf27e6cfdb6e9b17e7d4de Mon Sep 17 00:00:00 2001 From: Chuck Grindel Date: Thu, 7 Dec 2023 17:37:40 -0700 Subject: [PATCH 20/63] Add CI Bazel config --- .bazelrc | 5 +++++ .github/workflows/ci.yaml | 5 +++++ 2 files changed, 10 insertions(+) diff --git a/.bazelrc b/.bazelrc index ebd91e5..6622176 100644 --- a/.bazelrc +++ b/.bazelrc @@ -1,5 +1,10 @@ build --enable_bzlmod +# CI Configuration +# ---------------- +ci:common --announce_rc +ci:test --test_output=errors --test_summary=terse + # Remote Cache Authentication # --------------------------- try-import %workspace%/.bazelrc.auth diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index 2cdbcad..fd31ea8 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -33,6 +33,11 @@ jobs: with: buildbuddy_api_key: ${{ secrets.BUILDBUDDY_API_KEY }} bazelrc_path: .bazelrc.auth + - name: Configure + run: | + cat >>.bazelrc.local < Date: Thu, 7 Dec 2023 17:41:06 -0700 Subject: [PATCH 21/63] Fix silly mistake --- .bazelrc | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.bazelrc b/.bazelrc index 6622176..8a5e4ca 100644 --- a/.bazelrc +++ b/.bazelrc @@ -2,8 +2,8 @@ build --enable_bzlmod # CI Configuration # ---------------- -ci:common --announce_rc -ci:test --test_output=errors --test_summary=terse +common:ci --announce_rc +test:ci --test_output=errors --test_summary=terse # Remote Cache Authentication # --------------------------- From 73562e2361c01263b7af37238a1d2f4b94afdf34 Mon Sep 17 00:00:00 2001 From: Chuck Grindel Date: Thu, 7 Dec 2023 17:50:30 -0700 Subject: [PATCH 22/63] Add integration test --- .github/workflows/ci.yaml | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index fd31ea8..7756b15 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -14,11 +14,11 @@ concurrency: jobs: unit_tests: - name: Build & Test - Nixpkgs + name: Unit Tests strategy: fail-fast: false matrix: - os: [ubuntu-latest, macos-11] + os: [ubuntu-latest, macos-latest] runs-on: ${{ matrix.os }} steps: - name: Checkout the repository @@ -42,6 +42,18 @@ jobs: shell: bash run: | bazel test //... + - name: Execute simple + uses: ./ + with: + run: | + echo "FIRST" > integration_test.out + - name: Confirm output + shell: bash + run: | + output="$(&2 "FIRST was not found"; exit 1) + all_ci_tests: runs-on: ubuntu-latest From 21439aaf6664069022bdb31f21ac35c1b7f2086e Mon Sep 17 00:00:00 2001 From: Chuck Grindel Date: Thu, 7 Dec 2023 17:54:35 -0700 Subject: [PATCH 23/63] Specify GITHUB_ACTION_PATH --- .github/workflows/ci.yaml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index 7756b15..f1e70cc 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -43,6 +43,8 @@ jobs: run: | bazel test //... - name: Execute simple + env: + GITHUB_ACTION_PATH: . uses: ./ with: run: | From 34de990347c04ef00e252e818ea53a7fa347e6b6 Mon Sep 17 00:00:00 2001 From: Chuck Grindel Date: Thu, 7 Dec 2023 18:00:17 -0700 Subject: [PATCH 24/63] Add debug --- .github/workflows/ci.yaml | 2 -- action.yaml | 7 ++++++- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index f1e70cc..7756b15 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -43,8 +43,6 @@ jobs: run: | bazel test //... - name: Execute simple - env: - GITHUB_ACTION_PATH: . uses: ./ with: run: | diff --git a/action.yaml b/action.yaml index d57f414..963395d 100644 --- a/action.yaml +++ b/action.yaml @@ -16,4 +16,9 @@ runs: env: RNS_OPTS: ${{ inputs.options }} RNS_RUN: ${{ inputs.run }} - run: ${GITHUB_ACTION_PATH}/tools/run_nix_shell + # TODO: FIX ME! + # run: ${GITHUB_ACTION_PATH}/tools/run_nix_shell + run: | + echo >&2 "GITHUB_ACTION_PATH: ${GITHUB_ACTION_PATH:-}" + set -x + ${GITHUB_ACTION_PATH}/tools/run_nix_shell From bc573523eed9d7844003133ca0a0122fa870bf5d Mon Sep 17 00:00:00 2001 From: Chuck Grindel Date: Thu, 7 Dec 2023 18:02:01 -0700 Subject: [PATCH 25/63] Remove slash --- .github/workflows/ci.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index 7756b15..564ea8a 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -43,7 +43,7 @@ jobs: run: | bazel test //... - name: Execute simple - uses: ./ + uses: . with: run: | echo "FIRST" > integration_test.out From bcd39078233144f482a9d004dd86ae5345a18e22 Mon Sep 17 00:00:00 2001 From: Chuck Grindel Date: Thu, 7 Dec 2023 18:05:08 -0700 Subject: [PATCH 26/63] Add slash back --- .github/workflows/ci.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index 564ea8a..7756b15 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -43,7 +43,7 @@ jobs: run: | bazel test //... - name: Execute simple - uses: . + uses: ./ with: run: | echo "FIRST" > integration_test.out From 3b986e238fda871e8ab4b69ebff264fcd9432595 Mon Sep 17 00:00:00 2001 From: Chuck Grindel Date: Thu, 7 Dec 2023 18:11:28 -0700 Subject: [PATCH 27/63] Fix path to script --- action.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/action.yaml b/action.yaml index 963395d..836dd6d 100644 --- a/action.yaml +++ b/action.yaml @@ -21,4 +21,4 @@ runs: run: | echo >&2 "GITHUB_ACTION_PATH: ${GITHUB_ACTION_PATH:-}" set -x - ${GITHUB_ACTION_PATH}/tools/run_nix_shell + ${GITHUB_ACTION_PATH}/tools/run_nix_shell.sh From b8f0a15e38f3812a6400d8aff8890d11d227d0b5 Mon Sep 17 00:00:00 2001 From: Chuck Grindel Date: Thu, 7 Dec 2023 18:13:37 -0700 Subject: [PATCH 28/63] Clean up --- action.yaml | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/action.yaml b/action.yaml index 836dd6d..38d2dc4 100644 --- a/action.yaml +++ b/action.yaml @@ -16,9 +16,4 @@ runs: env: RNS_OPTS: ${{ inputs.options }} RNS_RUN: ${{ inputs.run }} - # TODO: FIX ME! - # run: ${GITHUB_ACTION_PATH}/tools/run_nix_shell - run: | - echo >&2 "GITHUB_ACTION_PATH: ${GITHUB_ACTION_PATH:-}" - set -x - ${GITHUB_ACTION_PATH}/tools/run_nix_shell.sh + run: ${GITHUB_ACTION_PATH}/tools/run_nix_shell.sh From 7714c1b0f7dae6d8f430703bf34b016c44c6b7a3 Mon Sep 17 00:00:00 2001 From: Chuck Grindel Date: Thu, 7 Dec 2023 18:14:42 -0700 Subject: [PATCH 29/63] Debug code --- tools/run_nix_shell.sh | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/tools/run_nix_shell.sh b/tools/run_nix_shell.sh index 849ff4e..cec33a1 100755 --- a/tools/run_nix_shell.sh +++ b/tools/run_nix_shell.sh @@ -54,6 +54,11 @@ fi # MARK - Execute script +# DEBUG BEGIN +echo >&2 "*** CHUCK $(basename "${BASH_SOURCE[0]}") =======" +ls -l >&2 +# DEBUG END + cmd=( nix-shell --pure ) if [[ ${#nix_shell_opts[@]} -gt 0 ]]; then cmd+=( "${nix_shell_opts[@]}" ) From 7c7ffbf4cabb5984f888f38d973c9da93b56f168 Mon Sep 17 00:00:00 2001 From: Chuck Grindel Date: Fri, 8 Dec 2023 06:56:00 -0700 Subject: [PATCH 30/63] Point to nixpkgs.nix --- .github/workflows/ci.yaml | 2 +- default.nix | 11 ----------- 2 files changed, 1 insertion(+), 12 deletions(-) delete mode 100644 default.nix diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index 7756b15..b365bd2 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -25,7 +25,7 @@ jobs: uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4 - uses: cachix/install-nix-action@v24 with: - nix_path: nixpkgs=./default.nix + nix_path: nixpkgs=./nixpkgs.nix extra_nix_config: | trusted-public-keys = cache.nixos.org-1:6NCHdD59X431o0gWypbMrAURkbJ16ZPMQFGspcDShjY= hydra.iohk.io:f/Ea+s+dFdN+3Y/G+FDgSq+a5NEWhJGzdjvKNGv0/EQ= extra-substituters = https://cache.iog.io diff --git a/default.nix b/default.nix deleted file mode 100644 index fecd44a..0000000 --- a/default.nix +++ /dev/null @@ -1,11 +0,0 @@ -# This exists so that we can set up the Nix environment in CI using cachix/install-nix-action. -(import - ( - let lock = builtins.fromJSON (builtins.readFile ./flake.lock); in - fetchTarball { - url = lock.nodes.flake-compat.locked.url or "https://github.com/edolstra/flake-compat/archive/${lock.nodes.flake-compat.locked.rev}.tar.gz"; - sha256 = lock.nodes.flake-compat.locked.narHash; - } - ) - { src = ./.; } -).defaultNix From 4a198ffd06864d52096ab99dfad3ca62ebc90eff Mon Sep 17 00:00:00 2001 From: Chuck Grindel Date: Fri, 8 Dec 2023 07:01:17 -0700 Subject: [PATCH 31/63] Add shell.nix to root so that we can execute nix-shell. --- shell.nix | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 shell.nix diff --git a/shell.nix b/shell.nix new file mode 100644 index 0000000..942ce01 --- /dev/null +++ b/shell.nix @@ -0,0 +1,10 @@ +(import + ( + let lock = builtins.fromJSON (builtins.readFile ./flake.lock); in + fetchTarball { + url = lock.nodes.flake-compat.locked.url or "https://github.com/edolstra/flake-compat/archive/${lock.nodes.flake-compat.locked.rev}.tar.gz"; + sha256 = lock.nodes.flake-compat.locked.narHash; + } + ) + { src = ./.; } +).shellNix From 239c1d83c675691097b46269d3117ef80a0655f8 Mon Sep 17 00:00:00 2001 From: Chuck Grindel Date: Fri, 8 Dec 2023 07:19:31 -0700 Subject: [PATCH 32/63] Refactor integration tests to their own jobs --- .github/actions/set_up_runner/action.yaml | 16 +++++++++++ .github/workflows/ci.yaml | 35 +++++++++++------------ 2 files changed, 32 insertions(+), 19 deletions(-) create mode 100644 .github/actions/set_up_runner/action.yaml diff --git a/.github/actions/set_up_runner/action.yaml b/.github/actions/set_up_runner/action.yaml new file mode 100644 index 0000000..f60b2e8 --- /dev/null +++ b/.github/actions/set_up_runner/action.yaml @@ -0,0 +1,16 @@ +name: Set up GitHub runner + +runs: + using: composite + steps: + - uses: cachix/install-nix-action@v24 + with: + nix_path: nixpkgs=./nixpkgs.nix + extra_nix_config: | + trusted-public-keys = cache.nixos.org-1:6NCHdD59X431o0gWypbMrAURkbJ16ZPMQFGspcDShjY= hydra.iohk.io:f/Ea+s+dFdN+3Y/G+FDgSq+a5NEWhJGzdjvKNGv0/EQ= + extra-substituters = https://cache.iog.io + - name: Configure + run: | + cat >>.bazelrc.local <>.bazelrc.local < integration_test.out + run: echo "FIRST" > integration_test.out - name: Confirm output shell: bash run: | @@ -59,6 +55,7 @@ jobs: runs-on: ubuntu-latest needs: - unit_tests + - integration_tests if: ${{ always() }} steps: - uses: cgrindel/gha_join_jobs@794a2d117251f22607f1aab937d3fd3eaaf9a2f5 # v1 From 690cc48fcb3d6440f7cc27ac6099ae4b4ea1fa46 Mon Sep 17 00:00:00 2001 From: Chuck Grindel Date: Fri, 8 Dec 2023 07:20:53 -0700 Subject: [PATCH 33/63] Add shell attribute --- .github/actions/set_up_runner/action.yaml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/actions/set_up_runner/action.yaml b/.github/actions/set_up_runner/action.yaml index f60b2e8..d0863fd 100644 --- a/.github/actions/set_up_runner/action.yaml +++ b/.github/actions/set_up_runner/action.yaml @@ -10,6 +10,7 @@ runs: trusted-public-keys = cache.nixos.org-1:6NCHdD59X431o0gWypbMrAURkbJ16ZPMQFGspcDShjY= hydra.iohk.io:f/Ea+s+dFdN+3Y/G+FDgSq+a5NEWhJGzdjvKNGv0/EQ= extra-substituters = https://cache.iog.io - name: Configure + shell: bash run: | cat >>.bazelrc.local < Date: Fri, 8 Dec 2023 07:32:26 -0700 Subject: [PATCH 34/63] Move shell.nix to integration tests directory --- tests/integration_tests/README.md | 4 ++++ shell.nix => tests/integration_tests/shell.nix | 4 ++-- 2 files changed, 6 insertions(+), 2 deletions(-) create mode 100644 tests/integration_tests/README.md rename shell.nix => tests/integration_tests/shell.nix (71%) diff --git a/tests/integration_tests/README.md b/tests/integration_tests/README.md new file mode 100644 index 0000000..b625e29 --- /dev/null +++ b/tests/integration_tests/README.md @@ -0,0 +1,4 @@ +# Integration Tests + +This directory contains the files needed to execute the integration tests as configured in the CI +GitHub workflow. diff --git a/shell.nix b/tests/integration_tests/shell.nix similarity index 71% rename from shell.nix rename to tests/integration_tests/shell.nix index 942ce01..1e8ad2e 100644 --- a/shell.nix +++ b/tests/integration_tests/shell.nix @@ -1,10 +1,10 @@ (import ( - let lock = builtins.fromJSON (builtins.readFile ./flake.lock); in + let lock = builtins.fromJSON (builtins.readFile ./../../flake.lock); in fetchTarball { url = lock.nodes.flake-compat.locked.url or "https://github.com/edolstra/flake-compat/archive/${lock.nodes.flake-compat.locked.rev}.tar.gz"; sha256 = lock.nodes.flake-compat.locked.narHash; } ) - { src = ./.; } + { src = ./../../.; } ).shellNix From 2725e6db7e635a6be25291db1e695e7801d33e96 Mon Sep 17 00:00:00 2001 From: Chuck Grindel Date: Fri, 8 Dec 2023 08:14:36 -0700 Subject: [PATCH 35/63] Incorporate variables into shell.nix --- tests/integration_tests/shell.nix | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/tests/integration_tests/shell.nix b/tests/integration_tests/shell.nix index 1e8ad2e..fa7d8ec 100644 --- a/tests/integration_tests/shell.nix +++ b/tests/integration_tests/shell.nix @@ -1,3 +1,7 @@ +{ + customVarBool ? false, + customVarStr ? "default", +}: (import ( let lock = builtins.fromJSON (builtins.readFile ./../../flake.lock); in @@ -7,4 +11,18 @@ } ) { src = ./../../.; } -).shellNix +).shellNix // ( + let pkgs = import ( + let lock = builtins.fromJSON (builtins.readFile ./../../flake.lock); in + fetchTarball { + url = "https://github.com/NixOS/nixpkgs/archive/${lock.nodes.nixpkgs.locked.rev}.tar.gz"; + sha256 = lock.nodes.nixpkgs.locked.narHash; + } + ) {}; in + with pkgs; + mkShell { + CUSTOM_VAR_BOOL = if customVarBool then "true" else "false"; + CUSTOM_VAR_STR = customVarStr; + # packages = [ nix ]; + } +) From 44eeb7366d11e4d3eaed5e5b12d5fea64e8a0eba Mon Sep 17 00:00:00 2001 From: Chuck Grindel Date: Fri, 8 Dec 2023 08:17:41 -0700 Subject: [PATCH 36/63] Refactored lock to be an arg --- tests/integration_tests/shell.nix | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/tests/integration_tests/shell.nix b/tests/integration_tests/shell.nix index fa7d8ec..d802366 100644 --- a/tests/integration_tests/shell.nix +++ b/tests/integration_tests/shell.nix @@ -1,10 +1,10 @@ { customVarBool ? false, customVarStr ? "default", + lock ? builtins.fromJSON (builtins.readFile ./../../flake.lock), }: (import ( - let lock = builtins.fromJSON (builtins.readFile ./../../flake.lock); in fetchTarball { url = lock.nodes.flake-compat.locked.url or "https://github.com/edolstra/flake-compat/archive/${lock.nodes.flake-compat.locked.rev}.tar.gz"; sha256 = lock.nodes.flake-compat.locked.narHash; @@ -13,7 +13,6 @@ { src = ./../../.; } ).shellNix // ( let pkgs = import ( - let lock = builtins.fromJSON (builtins.readFile ./../../flake.lock); in fetchTarball { url = "https://github.com/NixOS/nixpkgs/archive/${lock.nodes.nixpkgs.locked.rev}.tar.gz"; sha256 = lock.nodes.nixpkgs.locked.narHash; @@ -23,6 +22,5 @@ mkShell { CUSTOM_VAR_BOOL = if customVarBool then "true" else "false"; CUSTOM_VAR_STR = customVarStr; - # packages = [ nix ]; } ) From 6e37a440e97633397b0fb7172fd29894e8d27968 Mon Sep 17 00:00:00 2001 From: Chuck Grindel Date: Fri, 8 Dec 2023 08:22:57 -0700 Subject: [PATCH 37/63] Update integration test --- .github/workflows/ci.yaml | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index d135e97..ef052dd 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -41,15 +41,27 @@ jobs: - uses: ./.github/actions/set_up_runner - name: Execute simple uses: ./ + working-directory: ./tests/integration_tests with: - run: echo "FIRST" > integration_test.out + options: | + --arg customVarArg true + --argstr customVarStr "Hello, World!" + run: | + echo "FIRST" > integration_test.out + echo "${CUSTOM_VAR_BOOL}" >> integration_test.out + echo "${CUSTOM_VAR_STR}" >> integration_test.out - name: Confirm output shell: bash run: | output="$(&2 "FIRST was not found"; exit 1) - + expected="$(cat <<-EOF + FIRST + true + Hello, World! + EOF + )" + [[ "${output}" == "${expected}" ]] || \ + (echo >&2 "Ouptput from integration test does not match:" "${output}"; exit 1) all_ci_tests: runs-on: ubuntu-latest From f51ad99fbe725a0498ea822b2e8c09ef897c196a Mon Sep 17 00:00:00 2001 From: Chuck Grindel Date: Fri, 8 Dec 2023 08:42:04 -0700 Subject: [PATCH 38/63] Add support for working-directory --- .github/workflows/ci.yaml | 2 +- action.yaml | 6 ++++++ tests/tools_tests/run_nix_shell_test.sh | 14 ++++++++++++++ tools/run_nix_shell.sh | 10 ++++++++++ 4 files changed, 31 insertions(+), 1 deletion(-) diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index ef052dd..ee55db5 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -41,8 +41,8 @@ jobs: - uses: ./.github/actions/set_up_runner - name: Execute simple uses: ./ - working-directory: ./tests/integration_tests with: + working-directory: ./tests/integration_tests options: | --arg customVarArg true --argstr customVarStr "Hello, World!" diff --git a/action.yaml b/action.yaml index 38d2dc4..2380ac0 100644 --- a/action.yaml +++ b/action.yaml @@ -8,12 +8,18 @@ inputs: description: The path to a file that contains shell code or the actual shell code. options: type: string + description: Any parameters that are to be passed to nix-shell are specified here. + working-directory: + type: string + description: The path where the nix-shell execution should take place. + default: . runs: using: composite steps: - shell: bash env: + RNS_CWD: ${{ inputs.working-directory }} RNS_OPTS: ${{ inputs.options }} RNS_RUN: ${{ inputs.run }} run: ${GITHUB_ACTION_PATH}/tools/run_nix_shell.sh diff --git a/tests/tools_tests/run_nix_shell_test.sh b/tests/tools_tests/run_nix_shell_test.sh index f8c8727..ce96af5 100755 --- a/tests/tools_tests/run_nix_shell_test.sh +++ b/tests/tools_tests/run_nix_shell_test.sh @@ -100,3 +100,17 @@ RNS_OPTS='--arg customVarBool true --argstr customVarStr "This is a custom value )" assert_match "true" "${output}" "${assert_msg}" assert_match "This is a custom value" "${output}" "${assert_msg}" + +# Set up a new working directory +cwd="${PWD}/working-dir" +rm -rf "${cwd}" +mkdir -p "${cwd}" +cp shell.nix "${cwd}/" + +assert_msg="set working-directory flag" +output="$( "${run_nix_shell_sh}" --working-directory "${cwd}" 'echo "${PWD}"' )" +assert_equal "${cwd}" "${output}" "${assert_msg}" + +assert_msg="set RNS_CWD" +output="$( RNS_CWD="${cwd}" "${run_nix_shell_sh}" 'echo "${PWD}"' )" +assert_equal "${cwd}" "${output}" "${assert_msg}" diff --git a/tools/run_nix_shell.sh b/tools/run_nix_shell.sh index cec33a1..02ca4ed 100755 --- a/tools/run_nix_shell.sh +++ b/tools/run_nix_shell.sh @@ -12,6 +12,7 @@ fail() { # MARK - Arguments +cwd="${RNS_CWD:-}" script="${RNS_RUN:-}" nix_shell_opts=() @@ -24,6 +25,10 @@ while (("$#")); do nix_shell_opts+=( "${1}" "${2}" "${3}" ) shift 3 ;; + --working-directory) + cwd="${2}" + shift 2 + ;; *) if [[ -z "${script:-}" ]]; then script="${1}" @@ -59,6 +64,11 @@ echo >&2 "*** CHUCK $(basename "${BASH_SOURCE[0]}") =======" ls -l >&2 # DEBUG END +# Change to the specified working directory +if [[ -n "${cwd:-}" ]]; then + cd "${cwd}" +fi + cmd=( nix-shell --pure ) if [[ ${#nix_shell_opts[@]} -gt 0 ]]; then cmd+=( "${nix_shell_opts[@]}" ) From 78072eb2d12b7fde1f8957c9be5d7d8b1ee553ad Mon Sep 17 00:00:00 2001 From: Chuck Grindel Date: Fri, 8 Dec 2023 08:45:15 -0700 Subject: [PATCH 39/63] Read output from correct directory --- .github/workflows/ci.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index ee55db5..4ed3f0c 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -53,7 +53,7 @@ jobs: - name: Confirm output shell: bash run: | - output="$(&1 1>/dev/null +)" +assert_match "RNS_CWD: .*working-dir" "${output}" "${assert_msg}" +assert_match "RNS_OPTS: --arg customVarBool true" "${output}" "${assert_msg}" +assert_match "RNS_RUN: " "${output}" "${assert_msg}" +assert_match "cwd: .*working-dir" "${output}" "${assert_msg}" +assert_match 'nix_shell_opts: --argstr customVarStr This\\ is\\ a\\ custom\\ value\.' \ + "${output}" "${assert_msg}" +assert_match "script: echo HELLO" "${output}" "${assert_msg}" diff --git a/tools/run_nix_shell.sh b/tools/run_nix_shell.sh index 02ca4ed..9f9affc 100755 --- a/tools/run_nix_shell.sh +++ b/tools/run_nix_shell.sh @@ -5,8 +5,12 @@ set -o errexit -o nounset -o pipefail # NOTE: This script has been implemented so that it can be executed without Bazel. Do not pull in # external dependencies. -fail() { +warn() { echo >&2 "$@" +} + +fail() { + warn "$@" exit 1 } @@ -15,6 +19,7 @@ fail() { cwd="${RNS_CWD:-}" script="${RNS_RUN:-}" +verbose="${RNS_VERBOSE:-false}" nix_shell_opts=() while (("$#")); do case "${1}" in @@ -29,6 +34,10 @@ while (("$#")); do cwd="${2}" shift 2 ;; + --verbose) + verbose=true + shift 1 + ;; *) if [[ -z "${script:-}" ]]; then script="${1}" @@ -40,6 +49,23 @@ while (("$#")); do esac done +# MARK - Verbose + +if [[ "${verbose}" == "true" ]]; then + verbose_output="$(cat <<-EOF +RNS_CWD: ${RNS_CWD:-} +RNS_OPTS: ${RNS_OPTS:-} +RNS_RUN: ${RNS_RUN:-} +cwd: ${cwd:-} +nix_shell_opts: $( printf "%q " "${nix_shell_opts[@]}" ) +script: ${script:-} +EOF +)" + warn "${verbose_output}" +fi + +# MARK - Process Options and Arguments + # Look for any options passed via the RNS_OPTS environment variable. # Add them to the nix_shell_opts. if [[ -n "${RNS_OPTS:-}" ]]; then @@ -59,11 +85,6 @@ fi # MARK - Execute script -# DEBUG BEGIN -echo >&2 "*** CHUCK $(basename "${BASH_SOURCE[0]}") =======" -ls -l >&2 -# DEBUG END - # Change to the specified working directory if [[ -n "${cwd:-}" ]]; then cd "${cwd}" From 211b9b364f6c7ddfcd29da0c17057ed7beb3f655 Mon Sep 17 00:00:00 2001 From: Chuck Grindel Date: Fri, 8 Dec 2023 10:05:04 -0700 Subject: [PATCH 41/63] Enable verbose in the integration test --- .github/workflows/ci.yaml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index 4ed3f0c..99b93df 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -42,6 +42,7 @@ jobs: - name: Execute simple uses: ./ with: + verbose: true working-directory: ./tests/integration_tests options: | --arg customVarArg true From 51c8533aa8e0b976695c5c2915bbd386e62c9ef2 Mon Sep 17 00:00:00 2001 From: Chuck Grindel Date: Fri, 8 Dec 2023 10:06:54 -0700 Subject: [PATCH 42/63] Disable verbose --- .github/workflows/ci.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index 99b93df..1e6b4e7 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -42,7 +42,7 @@ jobs: - name: Execute simple uses: ./ with: - verbose: true + # verbose: true working-directory: ./tests/integration_tests options: | --arg customVarArg true From c48a789e82aa045b628ae455a3a019a9ceb706e6 Mon Sep 17 00:00:00 2001 From: Chuck Grindel Date: Fri, 8 Dec 2023 10:07:59 -0700 Subject: [PATCH 43/63] Enable verbose --- .github/workflows/ci.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index 1e6b4e7..99b93df 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -42,7 +42,7 @@ jobs: - name: Execute simple uses: ./ with: - # verbose: true + verbose: true working-directory: ./tests/integration_tests options: | --arg customVarArg true From 07654963c4280af2d9b758580424840613d6baec Mon Sep 17 00:00:00 2001 From: Chuck Grindel Date: Fri, 8 Dec 2023 10:13:12 -0700 Subject: [PATCH 44/63] Ensure output file for integration test does not exist. --- .github/workflows/ci.yaml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index 99b93df..ac119e1 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -39,6 +39,8 @@ jobs: - name: Checkout the repository uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4 - uses: ./.github/actions/set_up_runner + - shell: bash + run: rm -f ./tests/integration_tests/integration_test.out - name: Execute simple uses: ./ with: From 8e5e7a6ec7fefeb8070a733d064fb3e9b79921e2 Mon Sep 17 00:00:00 2001 From: Chuck Grindel Date: Fri, 8 Dec 2023 10:19:55 -0700 Subject: [PATCH 45/63] Debug code --- tests/integration_tests/shell.nix | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/tests/integration_tests/shell.nix b/tests/integration_tests/shell.nix index d802366..ca34b1d 100644 --- a/tests/integration_tests/shell.nix +++ b/tests/integration_tests/shell.nix @@ -12,15 +12,16 @@ ) { src = ./../../.; } ).shellNix // ( - let pkgs = import ( - fetchTarball { - url = "https://github.com/NixOS/nixpkgs/archive/${lock.nodes.nixpkgs.locked.rev}.tar.gz"; - sha256 = lock.nodes.nixpkgs.locked.narHash; - } - ) {}; in - with pkgs; - mkShell { - CUSTOM_VAR_BOOL = if customVarBool then "true" else "false"; - CUSTOM_VAR_STR = customVarStr; - } + # let pkgs = import ( + # fetchTarball { + # url = "https://github.com/NixOS/nixpkgs/archive/${lock.nodes.nixpkgs.locked.rev}.tar.gz"; + # sha256 = lock.nodes.nixpkgs.locked.narHash; + # } + # ) {}; in + # with pkgs; + # mkShell { + # CUSTOM_VAR_BOOL = if customVarBool then "true" else "false"; + # CUSTOM_VAR_STR = customVarStr; + # } + {} ) From 0c1819dc072c8e1a8081deb480eafba1c33cd9d5 Mon Sep 17 00:00:00 2001 From: Chuck Grindel Date: Fri, 8 Dec 2023 10:21:01 -0700 Subject: [PATCH 46/63] No need to delete the integration_test.out file --- .github/workflows/ci.yaml | 2 -- 1 file changed, 2 deletions(-) diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index ac119e1..99b93df 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -39,8 +39,6 @@ jobs: - name: Checkout the repository uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4 - uses: ./.github/actions/set_up_runner - - shell: bash - run: rm -f ./tests/integration_tests/integration_test.out - name: Execute simple uses: ./ with: From d5f20f14fc962b4eac20825cdf6a58cb585656c2 Mon Sep 17 00:00:00 2001 From: Chuck Grindel Date: Fri, 8 Dec 2023 10:25:47 -0700 Subject: [PATCH 47/63] Enable flakes --- .github/actions/set_up_runner/action.yaml | 7 ++++++- .github/workflows/ci.yaml | 4 ++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/.github/actions/set_up_runner/action.yaml b/.github/actions/set_up_runner/action.yaml index d0863fd..0e8ba98 100644 --- a/.github/actions/set_up_runner/action.yaml +++ b/.github/actions/set_up_runner/action.yaml @@ -1,11 +1,16 @@ name: Set up GitHub runner +inputs: + github_token: + type: string + runs: using: composite steps: - uses: cachix/install-nix-action@v24 with: - nix_path: nixpkgs=./nixpkgs.nix + # nix_path: nixpkgs=./nixpkgs.nix + github_access_token: ${{ inputs.github_token }} extra_nix_config: | trusted-public-keys = cache.nixos.org-1:6NCHdD59X431o0gWypbMrAURkbJ16ZPMQFGspcDShjY= hydra.iohk.io:f/Ea+s+dFdN+3Y/G+FDgSq+a5NEWhJGzdjvKNGv0/EQ= extra-substituters = https://cache.iog.io diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index 99b93df..42f7928 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -24,6 +24,8 @@ jobs: - name: Checkout the repository uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4 - uses: ./.github/actions/set_up_runner + with: + github_token: ${{ secrets.GITHUB_TOKEN }} - name: Execute Bazel tests shell: bash run: bazel test //... @@ -39,6 +41,8 @@ jobs: - name: Checkout the repository uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4 - uses: ./.github/actions/set_up_runner + with: + github_token: ${{ secrets.GITHUB_TOKEN }} - name: Execute simple uses: ./ with: From 5e1531bd5abb8929077a5f5521cb392cc3f56194 Mon Sep 17 00:00:00 2001 From: Chuck Grindel Date: Fri, 8 Dec 2023 10:39:20 -0700 Subject: [PATCH 48/63] Try using DeterminateSystems installer --- .github/actions/set_up_runner/action.yaml | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/.github/actions/set_up_runner/action.yaml b/.github/actions/set_up_runner/action.yaml index 0e8ba98..07aac4f 100644 --- a/.github/actions/set_up_runner/action.yaml +++ b/.github/actions/set_up_runner/action.yaml @@ -7,13 +7,16 @@ inputs: runs: using: composite steps: - - uses: cachix/install-nix-action@v24 + # - uses: cachix/install-nix-action@v24 + # with: + # # nix_path: nixpkgs=./nixpkgs.nix + # github_access_token: ${{ inputs.github_token }} + # extra_nix_config: | + # trusted-public-keys = cache.nixos.org-1:6NCHdD59X431o0gWypbMrAURkbJ16ZPMQFGspcDShjY= hydra.iohk.io:f/Ea+s+dFdN+3Y/G+FDgSq+a5NEWhJGzdjvKNGv0/EQ= + # extra-substituters = https://cache.iog.io + - uses: DeterminateSystems/nix-installer-action@v9 with: - # nix_path: nixpkgs=./nixpkgs.nix github_access_token: ${{ inputs.github_token }} - extra_nix_config: | - trusted-public-keys = cache.nixos.org-1:6NCHdD59X431o0gWypbMrAURkbJ16ZPMQFGspcDShjY= hydra.iohk.io:f/Ea+s+dFdN+3Y/G+FDgSq+a5NEWhJGzdjvKNGv0/EQ= - extra-substituters = https://cache.iog.io - name: Configure shell: bash run: | From e1b4c0917bb9b634ca41c0f4719ffd430321fa6a Mon Sep 17 00:00:00 2001 From: Chuck Grindel Date: Fri, 8 Dec 2023 10:41:20 -0700 Subject: [PATCH 49/63] Fix typo --- .github/actions/set_up_runner/action.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/actions/set_up_runner/action.yaml b/.github/actions/set_up_runner/action.yaml index 07aac4f..ab970d2 100644 --- a/.github/actions/set_up_runner/action.yaml +++ b/.github/actions/set_up_runner/action.yaml @@ -16,7 +16,7 @@ runs: # extra-substituters = https://cache.iog.io - uses: DeterminateSystems/nix-installer-action@v9 with: - github_access_token: ${{ inputs.github_token }} + github-token: ${{ inputs.github_token }} - name: Configure shell: bash run: | From 9711e74f891f4b7bc69778a41d0a7c1a11f04f98 Mon Sep 17 00:00:00 2001 From: Chuck Grindel Date: Fri, 8 Dec 2023 10:44:22 -0700 Subject: [PATCH 50/63] Put back original shell.nix --- tests/integration_tests/shell.nix | 23 +++++++++++------------ 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/tests/integration_tests/shell.nix b/tests/integration_tests/shell.nix index ca34b1d..d802366 100644 --- a/tests/integration_tests/shell.nix +++ b/tests/integration_tests/shell.nix @@ -12,16 +12,15 @@ ) { src = ./../../.; } ).shellNix // ( - # let pkgs = import ( - # fetchTarball { - # url = "https://github.com/NixOS/nixpkgs/archive/${lock.nodes.nixpkgs.locked.rev}.tar.gz"; - # sha256 = lock.nodes.nixpkgs.locked.narHash; - # } - # ) {}; in - # with pkgs; - # mkShell { - # CUSTOM_VAR_BOOL = if customVarBool then "true" else "false"; - # CUSTOM_VAR_STR = customVarStr; - # } - {} + let pkgs = import ( + fetchTarball { + url = "https://github.com/NixOS/nixpkgs/archive/${lock.nodes.nixpkgs.locked.rev}.tar.gz"; + sha256 = lock.nodes.nixpkgs.locked.narHash; + } + ) {}; in + with pkgs; + mkShell { + CUSTOM_VAR_BOOL = if customVarBool then "true" else "false"; + CUSTOM_VAR_STR = customVarStr; + } ) From 01110127dbe994c7ee78737e110e8f135bbf3eae Mon Sep 17 00:00:00 2001 From: Chuck Grindel Date: Fri, 8 Dec 2023 10:52:48 -0700 Subject: [PATCH 51/63] Add actual command-line invocation --- tools/run_nix_shell.sh | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/tools/run_nix_shell.sh b/tools/run_nix_shell.sh index 9f9affc..2a98660 100755 --- a/tools/run_nix_shell.sh +++ b/tools/run_nix_shell.sh @@ -51,14 +51,20 @@ done # MARK - Verbose -if [[ "${verbose}" == "true" ]]; then +is_verbose() { + [[ "${verbose}" == "true" ]] +} + +if is_verbose; then verbose_output="$(cat <<-EOF +=== run_nix_shell Inputs === RNS_CWD: ${RNS_CWD:-} RNS_OPTS: ${RNS_OPTS:-} RNS_RUN: ${RNS_RUN:-} cwd: ${cwd:-} nix_shell_opts: $( printf "%q " "${nix_shell_opts[@]}" ) script: ${script:-} +=== EOF )" warn "${verbose_output}" @@ -95,4 +101,15 @@ if [[ ${#nix_shell_opts[@]} -gt 0 ]]; then cmd+=( "${nix_shell_opts[@]}" ) fi cmd+=( --run "${script}" ) + +if is_verbose; then + verbose_output="$(cat <<-EOF +=== run_nix_shell command-line invocation === +$( printf "%q " "${cmd[@]}" ) +=== +EOF +)" + warn "${verbose_output}" +fi + "${cmd[@]}" From 33694d7b388409cb53d58c79c64b472dacf7d892 Mon Sep 17 00:00:00 2001 From: Chuck Grindel Date: Fri, 8 Dec 2023 10:57:28 -0700 Subject: [PATCH 52/63] Clean up --- .github/actions/set_up_runner/action.yaml | 7 ------- 1 file changed, 7 deletions(-) diff --git a/.github/actions/set_up_runner/action.yaml b/.github/actions/set_up_runner/action.yaml index ab970d2..93ccfef 100644 --- a/.github/actions/set_up_runner/action.yaml +++ b/.github/actions/set_up_runner/action.yaml @@ -7,13 +7,6 @@ inputs: runs: using: composite steps: - # - uses: cachix/install-nix-action@v24 - # with: - # # nix_path: nixpkgs=./nixpkgs.nix - # github_access_token: ${{ inputs.github_token }} - # extra_nix_config: | - # trusted-public-keys = cache.nixos.org-1:6NCHdD59X431o0gWypbMrAURkbJ16ZPMQFGspcDShjY= hydra.iohk.io:f/Ea+s+dFdN+3Y/G+FDgSq+a5NEWhJGzdjvKNGv0/EQ= - # extra-substituters = https://cache.iog.io - uses: DeterminateSystems/nix-installer-action@v9 with: github-token: ${{ inputs.github_token }} From c199b5fcdc5dd9dc2341e154a6f7d41dcee35923 Mon Sep 17 00:00:00 2001 From: Chuck Grindel Date: Fri, 8 Dec 2023 11:02:47 -0700 Subject: [PATCH 53/63] Fix typo --- .github/workflows/ci.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index 42f7928..729114a 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -49,7 +49,7 @@ jobs: verbose: true working-directory: ./tests/integration_tests options: | - --arg customVarArg true + --arg customVarBool true --argstr customVarStr "Hello, World!" run: | echo "FIRST" > integration_test.out From 5642b19889ce40e2f142360db5fa9e4f5c8c286c Mon Sep 17 00:00:00 2001 From: Chuck Grindel Date: Fri, 8 Dec 2023 11:07:06 -0700 Subject: [PATCH 54/63] Add magic cache --- .github/actions/set_up_runner/action.yaml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/actions/set_up_runner/action.yaml b/.github/actions/set_up_runner/action.yaml index 93ccfef..3ed27e2 100644 --- a/.github/actions/set_up_runner/action.yaml +++ b/.github/actions/set_up_runner/action.yaml @@ -10,6 +10,7 @@ runs: - uses: DeterminateSystems/nix-installer-action@v9 with: github-token: ${{ inputs.github_token }} + - uses: DeterminateSystems/magic-nix-cache-action@v2 - name: Configure shell: bash run: | From 2d009b89d9b6655717005d71032dd09b5145c952 Mon Sep 17 00:00:00 2001 From: Chuck Grindel Date: Fri, 8 Dec 2023 11:16:18 -0700 Subject: [PATCH 55/63] Refactor shell.nix --- tests/integration_tests/shell.nix | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/tests/integration_tests/shell.nix b/tests/integration_tests/shell.nix index d802366..fec72ce 100644 --- a/tests/integration_tests/shell.nix +++ b/tests/integration_tests/shell.nix @@ -2,22 +2,25 @@ customVarBool ? false, customVarStr ? "default", lock ? builtins.fromJSON (builtins.readFile ./../../flake.lock), -}: -(import - ( + flakeCompat ? ( fetchTarball { url = lock.nodes.flake-compat.locked.url or "https://github.com/edolstra/flake-compat/archive/${lock.nodes.flake-compat.locked.rev}.tar.gz"; sha256 = lock.nodes.flake-compat.locked.narHash; } - ) - { src = ./../../.; } -).shellNix // ( - let pkgs = import ( + ), + nixpkgs ? ( fetchTarball { url = "https://github.com/NixOS/nixpkgs/archive/${lock.nodes.nixpkgs.locked.rev}.tar.gz"; sha256 = lock.nodes.nixpkgs.locked.narHash; } - ) {}; in + ), +}: +( + # Call flakeCompat with src pointing to the location of the flake.nix. + import flakeCompat { src = ./../../.; } +).shellNix // ( + # This expression is generating a shell that is merged with the one provided by shellNix. + let pkgs = import nixpkgs {}; in with pkgs; mkShell { CUSTOM_VAR_BOOL = if customVarBool then "true" else "false"; From 9b5036c834b9fdf9eaec0e1cc8d0337c02eb4688 Mon Sep 17 00:00:00 2001 From: Chuck Grindel Date: Fri, 8 Dec 2023 11:54:17 -0700 Subject: [PATCH 56/63] Start refactor to separate tests. --- tests/tools_tests/BUILD.bazel | 34 ++--- tests/tools_tests/run_nix_shell_test.bzl | 31 +++++ tests/tools_tests/run_nix_shell_test.sh | 162 ++++++++++++----------- tests/tools_tests/simple_script_test.sh | 3 + 4 files changed, 135 insertions(+), 95 deletions(-) create mode 100644 tests/tools_tests/run_nix_shell_test.bzl create mode 100644 tests/tools_tests/simple_script_test.sh diff --git a/tests/tools_tests/BUILD.bazel b/tests/tools_tests/BUILD.bazel index b0a5b9a..6afab65 100644 --- a/tests/tools_tests/BUILD.bazel +++ b/tests/tools_tests/BUILD.bazel @@ -1,15 +1,19 @@ -sh_test( - name = "run_nix_shell_test", - srcs = ["run_nix_shell_test.sh"], - data = [ - "//:flake_files", - "//tools:run_nix_shell", - ], - # MacOS sandbox fails this test with the following error: - # sandbox-exec: sandbox_apply: Operation not permitted - tags = ["no-sandbox"], - deps = [ - "@bazel_tools//tools/bash/runfiles", - "@cgrindel_bazel_starlib//shlib/lib:assertions", - ], -) +load(":run_nix_shell_test.bzl", "run_nix_shell_test") + +# sh_test( +# name = "run_nix_shell_test", +# srcs = ["run_nix_shell_test.sh"], +# data = [ +# "//:flake_files", +# "//tools:run_nix_shell", +# ], +# # MacOS sandbox fails this test with the following error: +# # sandbox-exec: sandbox_apply: Operation not permitted +# tags = ["no-sandbox"], +# deps = [ +# "@bazel_tools//tools/bash/runfiles", +# "@cgrindel_bazel_starlib//shlib/lib:assertions", +# ], +# ) + +run_nix_shell_test(name = "simple_script") diff --git a/tests/tools_tests/run_nix_shell_test.bzl b/tests/tools_tests/run_nix_shell_test.bzl new file mode 100644 index 0000000..28e716b --- /dev/null +++ b/tests/tools_tests/run_nix_shell_test.bzl @@ -0,0 +1,31 @@ +def run_nix_shell_test(name, test_file = None, **kwargs): + if test_file == None: + test_file = "{}_test.sh".format(name) + + lib_name = name + "_library" + native.sh_library( + name = lib_name, + srcs = [test_file], + ) + + # lib_name_target = ":" + lib_name + native.sh_test( + name = name, + srcs = ["@run_nix_shell//tests/tools_tests:run_nix_shell_test.sh"], + args = [ + "$(location :{})".format(lib_name), + ], + data = [ + lib_name, + "//:flake_files", + "//tools:run_nix_shell", + ], + # MacOS sandbox fails this test with the following error: + # sandbox-exec: sandbox_apply: Operation not permitted + tags = ["no-sandbox"], + deps = [ + "@bazel_tools//tools/bash/runfiles", + "@cgrindel_bazel_starlib//shlib/lib:assertions", + ], + **kwargs + ) diff --git a/tests/tools_tests/run_nix_shell_test.sh b/tests/tools_tests/run_nix_shell_test.sh index bf91556..f210be7 100755 --- a/tests/tools_tests/run_nix_shell_test.sh +++ b/tests/tools_tests/run_nix_shell_test.sh @@ -51,83 +51,85 @@ EOF # MARK - Test -assert_msg="simple script" -output="$( "${run_nix_shell_sh}" 'echo "Hello, World!"' )" -assert_equal "Hello, World!" "${output}" "${assert_msg}" - -assert_msg="multi-line script" -output="$( -"${run_nix_shell_sh}" ' -echo "Hello, World!" -echo "Second Line" -' -)" -assert_match "Hello, World" "${output}" "${assert_msg}" -assert_match "Second Line" "${output}" "${assert_msg}" - -assert_msg="path to script file" -custom_script_path="./custom_script.sh" -cat >"${custom_script_path}" <<-EOF -#!/usr/bin/env bash -set -o errexit -o nounset -o pipefail -echo "Hello from custom script" -EOF -chmod +x "${custom_script_path}" -output="$( "${run_nix_shell_sh}" "${custom_script_path}" )" -assert_equal "Hello from custom script" "${output}" "${assert_msg}" - -assert_msg="default value for CUSTOM_VAR_BOOl" -output="$( "${run_nix_shell_sh}" 'echo "${CUSTOM_VAR_BOOL}"' )" -assert_equal "false" "${output}" "${assert_msg}" - -assert_msg="default value for CUSTOM_VAR_STR" -output="$( "${run_nix_shell_sh}" 'echo "${CUSTOM_VAR_STR}"' )" -assert_equal "default" "${output}" "${assert_msg}" - -assert_msg="custom value for CUSTOM_VAR_BOOL via command-line arg" -output="$( "${run_nix_shell_sh}" --arg customVarBool true 'echo "${CUSTOM_VAR_BOOL}"' )" -assert_equal "true" "${output}" "${assert_msg}" - -assert_msg="custom value for CUSTOM_VAR_STR via command-line arg" -expected="This is a custom value." -output="$( "${run_nix_shell_sh}" --argstr customVarStr "${expected}" 'echo "${CUSTOM_VAR_STR}"' )" -assert_equal "${expected}" "${output}" "${assert_msg}" - -assert_msg="custom value for CUSTOM_VAR_BOOL via RNS_OPT" -output="$( -RNS_OPTS='--arg customVarBool true --argstr customVarStr "This is a custom value."' \ - "${run_nix_shell_sh}" 'echo "${CUSTOM_VAR_BOOL}"; echo "${CUSTOM_VAR_STR}"' -)" -assert_match "true" "${output}" "${assert_msg}" -assert_match "This is a custom value" "${output}" "${assert_msg}" - -# Set up a new working directory -cwd="${PWD}/working-dir" -rm -rf "${cwd}" -mkdir -p "${cwd}" -cp shell.nix "${cwd}/" - -assert_msg="set working-directory flag" -output="$( "${run_nix_shell_sh}" --working-directory "${cwd}" 'echo "${PWD}"' )" -assert_equal "${cwd}" "${output}" "${assert_msg}" - -assert_msg="set RNS_CWD env var" -output="$( RNS_CWD="${cwd}" "${run_nix_shell_sh}" 'echo "${PWD}"' )" -assert_equal "${cwd}" "${output}" "${assert_msg}" - -assert_msg="set RNS_VERBOSE env var" -output="$( - RNS_VERBOSE="true" \ - RNS_CWD="${cwd}" \ - RNS_OPTS='--arg customVarBool true ' \ - "${run_nix_shell_sh}" \ - --argstr customVarStr "This is a custom value." \ - 'echo HELLO' 2>&1 1>/dev/null -)" -assert_match "RNS_CWD: .*working-dir" "${output}" "${assert_msg}" -assert_match "RNS_OPTS: --arg customVarBool true" "${output}" "${assert_msg}" -assert_match "RNS_RUN: " "${output}" "${assert_msg}" -assert_match "cwd: .*working-dir" "${output}" "${assert_msg}" -assert_match 'nix_shell_opts: --argstr customVarStr This\\ is\\ a\\ custom\\ value\.' \ - "${output}" "${assert_msg}" -assert_match "script: echo HELLO" "${output}" "${assert_msg}" +source "${1}" + +#assert_msg="simple script" +#output="$( "${run_nix_shell_sh}" 'echo "Hello, World!"' )" +#assert_equal "Hello, World!" "${output}" "${assert_msg}" + +#assert_msg="multi-line script" +#output="$( +#"${run_nix_shell_sh}" ' +#echo "Hello, World!" +#echo "Second Line" +#' +#)" +#assert_match "Hello, World" "${output}" "${assert_msg}" +#assert_match "Second Line" "${output}" "${assert_msg}" + +#assert_msg="path to script file" +#custom_script_path="./custom_script.sh" +#cat >"${custom_script_path}" <<-EOF +##!/usr/bin/env bash +#set -o errexit -o nounset -o pipefail +#echo "Hello from custom script" +#EOF +#chmod +x "${custom_script_path}" +#output="$( "${run_nix_shell_sh}" "${custom_script_path}" )" +#assert_equal "Hello from custom script" "${output}" "${assert_msg}" + +#assert_msg="default value for CUSTOM_VAR_BOOl" +#output="$( "${run_nix_shell_sh}" 'echo "${CUSTOM_VAR_BOOL}"' )" +#assert_equal "false" "${output}" "${assert_msg}" + +#assert_msg="default value for CUSTOM_VAR_STR" +#output="$( "${run_nix_shell_sh}" 'echo "${CUSTOM_VAR_STR}"' )" +#assert_equal "default" "${output}" "${assert_msg}" + +#assert_msg="custom value for CUSTOM_VAR_BOOL via command-line arg" +#output="$( "${run_nix_shell_sh}" --arg customVarBool true 'echo "${CUSTOM_VAR_BOOL}"' )" +#assert_equal "true" "${output}" "${assert_msg}" + +#assert_msg="custom value for CUSTOM_VAR_STR via command-line arg" +#expected="This is a custom value." +#output="$( "${run_nix_shell_sh}" --argstr customVarStr "${expected}" 'echo "${CUSTOM_VAR_STR}"' )" +#assert_equal "${expected}" "${output}" "${assert_msg}" + +#assert_msg="custom value for CUSTOM_VAR_BOOL via RNS_OPT" +#output="$( +#RNS_OPTS='--arg customVarBool true --argstr customVarStr "This is a custom value."' \ +# "${run_nix_shell_sh}" 'echo "${CUSTOM_VAR_BOOL}"; echo "${CUSTOM_VAR_STR}"' +#)" +#assert_match "true" "${output}" "${assert_msg}" +#assert_match "This is a custom value" "${output}" "${assert_msg}" + +## Set up a new working directory +#cwd="${PWD}/working-dir" +#rm -rf "${cwd}" +#mkdir -p "${cwd}" +#cp shell.nix "${cwd}/" + +#assert_msg="set working-directory flag" +#output="$( "${run_nix_shell_sh}" --working-directory "${cwd}" 'echo "${PWD}"' )" +#assert_equal "${cwd}" "${output}" "${assert_msg}" + +#assert_msg="set RNS_CWD env var" +#output="$( RNS_CWD="${cwd}" "${run_nix_shell_sh}" 'echo "${PWD}"' )" +#assert_equal "${cwd}" "${output}" "${assert_msg}" + +#assert_msg="set RNS_VERBOSE env var" +#output="$( +# RNS_VERBOSE="true" \ +# RNS_CWD="${cwd}" \ +# RNS_OPTS='--arg customVarBool true ' \ +# "${run_nix_shell_sh}" \ +# --argstr customVarStr "This is a custom value." \ +# 'echo HELLO' 2>&1 1>/dev/null +#)" +#assert_match "RNS_CWD: .*working-dir" "${output}" "${assert_msg}" +#assert_match "RNS_OPTS: --arg customVarBool true" "${output}" "${assert_msg}" +#assert_match "RNS_RUN: " "${output}" "${assert_msg}" +#assert_match "cwd: .*working-dir" "${output}" "${assert_msg}" +#assert_match 'nix_shell_opts: --argstr customVarStr This\\ is\\ a\\ custom\\ value\.' \ +# "${output}" "${assert_msg}" +#assert_match "script: echo HELLO" "${output}" "${assert_msg}" diff --git a/tests/tools_tests/simple_script_test.sh b/tests/tools_tests/simple_script_test.sh new file mode 100644 index 0000000..0c9bd03 --- /dev/null +++ b/tests/tools_tests/simple_script_test.sh @@ -0,0 +1,3 @@ +assert_msg="simple script" +output="$( "${run_nix_shell_sh}" 'echo "Hello, World!"' )" +assert_equal "Hello, World!" "${output}" "${assert_msg}" From 60977982954fdb78db33bc6ef1c34488f49e6d16 Mon Sep 17 00:00:00 2001 From: Chuck Grindel Date: Fri, 8 Dec 2023 12:10:11 -0700 Subject: [PATCH 57/63] Created separate tests --- tests/tools_tests/BUILD.bazel | 16 ++- tests/tools_tests/custom_var_bool_test.sh | 8 ++ tests/tools_tests/custom_var_str_test.sh | 8 ++ tests/tools_tests/multi_line_script_test.sh | 9 ++ tests/tools_tests/rns_opts_test.sh | 8 ++ tests/tools_tests/run_nix_shell_test.bzl | 4 +- tests/tools_tests/run_nix_shell_test.sh | 135 ------------------ .../tools_tests/run_nix_shell_test_runner.sh | 54 +++++++ tests/tools_tests/script_file_test.sh | 11 ++ tests/tools_tests/verbose_test.sh | 22 +++ tests/tools_tests/working_dir_test.sh | 13 ++ 11 files changed, 150 insertions(+), 138 deletions(-) create mode 100644 tests/tools_tests/custom_var_bool_test.sh create mode 100644 tests/tools_tests/custom_var_str_test.sh create mode 100644 tests/tools_tests/multi_line_script_test.sh create mode 100644 tests/tools_tests/rns_opts_test.sh delete mode 100755 tests/tools_tests/run_nix_shell_test.sh create mode 100755 tests/tools_tests/run_nix_shell_test_runner.sh create mode 100644 tests/tools_tests/script_file_test.sh create mode 100644 tests/tools_tests/verbose_test.sh create mode 100644 tests/tools_tests/working_dir_test.sh diff --git a/tests/tools_tests/BUILD.bazel b/tests/tools_tests/BUILD.bazel index 6afab65..4b2f8d7 100644 --- a/tests/tools_tests/BUILD.bazel +++ b/tests/tools_tests/BUILD.bazel @@ -16,4 +16,18 @@ load(":run_nix_shell_test.bzl", "run_nix_shell_test") # ], # ) -run_nix_shell_test(name = "simple_script") +run_nix_shell_test(name = "simple_script_test") + +run_nix_shell_test(name = "multi_line_script_test") + +run_nix_shell_test(name = "script_file_test") + +run_nix_shell_test(name = "custom_var_bool_test") + +run_nix_shell_test(name = "custom_var_str_test") + +run_nix_shell_test(name = "rns_opts_test") + +run_nix_shell_test(name = "working_dir_test") + +run_nix_shell_test(name = "verbose_test") diff --git a/tests/tools_tests/custom_var_bool_test.sh b/tests/tools_tests/custom_var_bool_test.sh new file mode 100644 index 0000000..246d201 --- /dev/null +++ b/tests/tools_tests/custom_var_bool_test.sh @@ -0,0 +1,8 @@ +assert_msg="default value for CUSTOM_VAR_BOOl" +output="$( "${run_nix_shell_sh}" 'echo "${CUSTOM_VAR_BOOL}"' )" +assert_equal "false" "${output}" "${assert_msg}" + +assert_msg="custom value for CUSTOM_VAR_BOOL via command-line arg" +output="$( "${run_nix_shell_sh}" --arg customVarBool true 'echo "${CUSTOM_VAR_BOOL}"' )" +assert_equal "true" "${output}" "${assert_msg}" + diff --git a/tests/tools_tests/custom_var_str_test.sh b/tests/tools_tests/custom_var_str_test.sh new file mode 100644 index 0000000..1149082 --- /dev/null +++ b/tests/tools_tests/custom_var_str_test.sh @@ -0,0 +1,8 @@ +assert_msg="default value for CUSTOM_VAR_STR" +output="$( "${run_nix_shell_sh}" 'echo "${CUSTOM_VAR_STR}"' )" +assert_equal "default" "${output}" "${assert_msg}" + +assert_msg="custom value for CUSTOM_VAR_STR via command-line arg" +expected="This is a custom value." +output="$( "${run_nix_shell_sh}" --argstr customVarStr "${expected}" 'echo "${CUSTOM_VAR_STR}"' )" +assert_equal "${expected}" "${output}" "${assert_msg}" diff --git a/tests/tools_tests/multi_line_script_test.sh b/tests/tools_tests/multi_line_script_test.sh new file mode 100644 index 0000000..7d6638e --- /dev/null +++ b/tests/tools_tests/multi_line_script_test.sh @@ -0,0 +1,9 @@ +assert_msg="multi-line script" +output="$( +"${run_nix_shell_sh}" ' +echo "Hello, World!" +echo "Second Line" +' +)" +assert_match "Hello, World" "${output}" "${assert_msg}" +assert_match "Second Line" "${output}" "${assert_msg}" diff --git a/tests/tools_tests/rns_opts_test.sh b/tests/tools_tests/rns_opts_test.sh new file mode 100644 index 0000000..1b9b267 --- /dev/null +++ b/tests/tools_tests/rns_opts_test.sh @@ -0,0 +1,8 @@ +assert_msg="custom value for CUSTOM_VAR_BOOL via RNS_OPTS" +output="$( +RNS_OPTS='--arg customVarBool true --argstr customVarStr "This is a custom value."' \ + "${run_nix_shell_sh}" 'echo "${CUSTOM_VAR_BOOL}"; echo "${CUSTOM_VAR_STR}"' +)" +assert_match "true" "${output}" "${assert_msg}" +assert_match "This is a custom value" "${output}" "${assert_msg}" + diff --git a/tests/tools_tests/run_nix_shell_test.bzl b/tests/tools_tests/run_nix_shell_test.bzl index 28e716b..aafed06 100644 --- a/tests/tools_tests/run_nix_shell_test.bzl +++ b/tests/tools_tests/run_nix_shell_test.bzl @@ -1,6 +1,6 @@ def run_nix_shell_test(name, test_file = None, **kwargs): if test_file == None: - test_file = "{}_test.sh".format(name) + test_file = "{}.sh".format(name) lib_name = name + "_library" native.sh_library( @@ -11,7 +11,7 @@ def run_nix_shell_test(name, test_file = None, **kwargs): # lib_name_target = ":" + lib_name native.sh_test( name = name, - srcs = ["@run_nix_shell//tests/tools_tests:run_nix_shell_test.sh"], + srcs = ["@run_nix_shell//tests/tools_tests:run_nix_shell_test_runner.sh"], args = [ "$(location :{})".format(lib_name), ], diff --git a/tests/tools_tests/run_nix_shell_test.sh b/tests/tools_tests/run_nix_shell_test.sh deleted file mode 100755 index f210be7..0000000 --- a/tests/tools_tests/run_nix_shell_test.sh +++ /dev/null @@ -1,135 +0,0 @@ -#!/usr/bin/env bash - -# --- begin runfiles.bash initialization v2 --- -# Copy-pasted from the Bazel Bash runfiles library v2. -set -o nounset -o pipefail; f=bazel_tools/tools/bash/runfiles/runfiles.bash -# shellcheck disable=SC1090 -source "${RUNFILES_DIR:-/dev/null}/$f" 2>/dev/null || \ - source "$(grep -sm1 "^$f " "${RUNFILES_MANIFEST_FILE:-/dev/null}" | cut -f2- -d' ')" 2>/dev/null || \ - source "$0.runfiles/$f" 2>/dev/null || \ - source "$(grep -sm1 "^$f " "$0.runfiles_manifest" | cut -f2- -d' ')" 2>/dev/null || \ - source "$(grep -sm1 "^$f " "$0.exe.runfiles_manifest" | cut -f2- -d' ')" 2>/dev/null || \ - { echo>&2 "ERROR: cannot find $f"; exit 1; }; f=; set -o errexit -# --- end runfiles.bash initialization v2 --- - -# MARK - Locate Deps - -assertions_sh_location=cgrindel_bazel_starlib/shlib/lib/assertions.sh -assertions_sh="$(rlocation "${assertions_sh_location}")" || \ - (echo >&2 "Failed to locate ${assertions_sh_location}" && exit 1) -source "${assertions_sh}" - -run_nix_shell_sh_location=run_nix_shell/tools/run_nix_shell.sh -run_nix_shell_sh="$(rlocation "${run_nix_shell_sh_location}")" || \ - (echo >&2 "Failed to locate ${run_nix_shell_sh_location}" && exit 1) - -flake_lock_location=run_nix_shell/flake.lock -flake_lock="$(rlocation "${flake_lock_location}")" || \ - (echo >&2 "Failed to locate ${flake_lock_location}" && exit 1) - -# MARK - Setup - -cat >shell.nix <<-EOF -{ - pkgs ? import ( - let lock = builtins.fromJSON (builtins.readFile ${flake_lock}); in - fetchTarball { - url = "https://github.com/NixOS/nixpkgs/archive/\${lock.nodes.nixpkgs.locked.rev}.tar.gz"; - sha256 = lock.nodes.nixpkgs.locked.narHash; - } - ) {}, - customVarBool ? false, - customVarStr ? "default", -}: -with pkgs; -mkShell { - CUSTOM_VAR_BOOL = if customVarBool then "true" else "false"; - CUSTOM_VAR_STR = customVarStr; - packages = [ nix ]; -} -EOF - -# MARK - Test - -source "${1}" - -#assert_msg="simple script" -#output="$( "${run_nix_shell_sh}" 'echo "Hello, World!"' )" -#assert_equal "Hello, World!" "${output}" "${assert_msg}" - -#assert_msg="multi-line script" -#output="$( -#"${run_nix_shell_sh}" ' -#echo "Hello, World!" -#echo "Second Line" -#' -#)" -#assert_match "Hello, World" "${output}" "${assert_msg}" -#assert_match "Second Line" "${output}" "${assert_msg}" - -#assert_msg="path to script file" -#custom_script_path="./custom_script.sh" -#cat >"${custom_script_path}" <<-EOF -##!/usr/bin/env bash -#set -o errexit -o nounset -o pipefail -#echo "Hello from custom script" -#EOF -#chmod +x "${custom_script_path}" -#output="$( "${run_nix_shell_sh}" "${custom_script_path}" )" -#assert_equal "Hello from custom script" "${output}" "${assert_msg}" - -#assert_msg="default value for CUSTOM_VAR_BOOl" -#output="$( "${run_nix_shell_sh}" 'echo "${CUSTOM_VAR_BOOL}"' )" -#assert_equal "false" "${output}" "${assert_msg}" - -#assert_msg="default value for CUSTOM_VAR_STR" -#output="$( "${run_nix_shell_sh}" 'echo "${CUSTOM_VAR_STR}"' )" -#assert_equal "default" "${output}" "${assert_msg}" - -#assert_msg="custom value for CUSTOM_VAR_BOOL via command-line arg" -#output="$( "${run_nix_shell_sh}" --arg customVarBool true 'echo "${CUSTOM_VAR_BOOL}"' )" -#assert_equal "true" "${output}" "${assert_msg}" - -#assert_msg="custom value for CUSTOM_VAR_STR via command-line arg" -#expected="This is a custom value." -#output="$( "${run_nix_shell_sh}" --argstr customVarStr "${expected}" 'echo "${CUSTOM_VAR_STR}"' )" -#assert_equal "${expected}" "${output}" "${assert_msg}" - -#assert_msg="custom value for CUSTOM_VAR_BOOL via RNS_OPT" -#output="$( -#RNS_OPTS='--arg customVarBool true --argstr customVarStr "This is a custom value."' \ -# "${run_nix_shell_sh}" 'echo "${CUSTOM_VAR_BOOL}"; echo "${CUSTOM_VAR_STR}"' -#)" -#assert_match "true" "${output}" "${assert_msg}" -#assert_match "This is a custom value" "${output}" "${assert_msg}" - -## Set up a new working directory -#cwd="${PWD}/working-dir" -#rm -rf "${cwd}" -#mkdir -p "${cwd}" -#cp shell.nix "${cwd}/" - -#assert_msg="set working-directory flag" -#output="$( "${run_nix_shell_sh}" --working-directory "${cwd}" 'echo "${PWD}"' )" -#assert_equal "${cwd}" "${output}" "${assert_msg}" - -#assert_msg="set RNS_CWD env var" -#output="$( RNS_CWD="${cwd}" "${run_nix_shell_sh}" 'echo "${PWD}"' )" -#assert_equal "${cwd}" "${output}" "${assert_msg}" - -#assert_msg="set RNS_VERBOSE env var" -#output="$( -# RNS_VERBOSE="true" \ -# RNS_CWD="${cwd}" \ -# RNS_OPTS='--arg customVarBool true ' \ -# "${run_nix_shell_sh}" \ -# --argstr customVarStr "This is a custom value." \ -# 'echo HELLO' 2>&1 1>/dev/null -#)" -#assert_match "RNS_CWD: .*working-dir" "${output}" "${assert_msg}" -#assert_match "RNS_OPTS: --arg customVarBool true" "${output}" "${assert_msg}" -#assert_match "RNS_RUN: " "${output}" "${assert_msg}" -#assert_match "cwd: .*working-dir" "${output}" "${assert_msg}" -#assert_match 'nix_shell_opts: --argstr customVarStr This\\ is\\ a\\ custom\\ value\.' \ -# "${output}" "${assert_msg}" -#assert_match "script: echo HELLO" "${output}" "${assert_msg}" diff --git a/tests/tools_tests/run_nix_shell_test_runner.sh b/tests/tools_tests/run_nix_shell_test_runner.sh new file mode 100755 index 0000000..1f2536b --- /dev/null +++ b/tests/tools_tests/run_nix_shell_test_runner.sh @@ -0,0 +1,54 @@ +#!/usr/bin/env bash + +# --- begin runfiles.bash initialization v2 --- +# Copy-pasted from the Bazel Bash runfiles library v2. +set -o nounset -o pipefail; f=bazel_tools/tools/bash/runfiles/runfiles.bash +# shellcheck disable=SC1090 +source "${RUNFILES_DIR:-/dev/null}/$f" 2>/dev/null || \ + source "$(grep -sm1 "^$f " "${RUNFILES_MANIFEST_FILE:-/dev/null}" | cut -f2- -d' ')" 2>/dev/null || \ + source "$0.runfiles/$f" 2>/dev/null || \ + source "$(grep -sm1 "^$f " "$0.runfiles_manifest" | cut -f2- -d' ')" 2>/dev/null || \ + source "$(grep -sm1 "^$f " "$0.exe.runfiles_manifest" | cut -f2- -d' ')" 2>/dev/null || \ + { echo>&2 "ERROR: cannot find $f"; exit 1; }; f=; set -o errexit +# --- end runfiles.bash initialization v2 --- + +# MARK - Locate Deps + +assertions_sh_location=cgrindel_bazel_starlib/shlib/lib/assertions.sh +assertions_sh="$(rlocation "${assertions_sh_location}")" || \ + (echo >&2 "Failed to locate ${assertions_sh_location}" && exit 1) +source "${assertions_sh}" + +run_nix_shell_sh_location=run_nix_shell/tools/run_nix_shell.sh +run_nix_shell_sh="$(rlocation "${run_nix_shell_sh_location}")" || \ + (echo >&2 "Failed to locate ${run_nix_shell_sh_location}" && exit 1) + +flake_lock_location=run_nix_shell/flake.lock +flake_lock="$(rlocation "${flake_lock_location}")" || \ + (echo >&2 "Failed to locate ${flake_lock_location}" && exit 1) + +# MARK - Setup + +cat >shell.nix <<-EOF +{ + pkgs ? import ( + let lock = builtins.fromJSON (builtins.readFile ${flake_lock}); in + fetchTarball { + url = "https://github.com/NixOS/nixpkgs/archive/\${lock.nodes.nixpkgs.locked.rev}.tar.gz"; + sha256 = lock.nodes.nixpkgs.locked.narHash; + } + ) {}, + customVarBool ? false, + customVarStr ? "default", +}: +with pkgs; +mkShell { + CUSTOM_VAR_BOOL = if customVarBool then "true" else "false"; + CUSTOM_VAR_STR = customVarStr; + packages = [ nix ]; +} +EOF + +# MARK - Test + +source "${1}" diff --git a/tests/tools_tests/script_file_test.sh b/tests/tools_tests/script_file_test.sh new file mode 100644 index 0000000..2227558 --- /dev/null +++ b/tests/tools_tests/script_file_test.sh @@ -0,0 +1,11 @@ +assert_msg="path to script file" +custom_script_path="./custom_script.sh" +cat >"${custom_script_path}" <<-EOF +#!/usr/bin/env bash +set -o errexit -o nounset -o pipefail +echo "Hello from custom script" +EOF +chmod +x "${custom_script_path}" +output="$( "${run_nix_shell_sh}" "${custom_script_path}" )" +assert_equal "Hello from custom script" "${output}" "${assert_msg}" + diff --git a/tests/tools_tests/verbose_test.sh b/tests/tools_tests/verbose_test.sh new file mode 100644 index 0000000..38a95e2 --- /dev/null +++ b/tests/tools_tests/verbose_test.sh @@ -0,0 +1,22 @@ +# Set up a new working directory +cwd="${PWD}/working-dir" +rm -rf "${cwd}" +mkdir -p "${cwd}" +cp shell.nix "${cwd}/" + +assert_msg="set RNS_VERBOSE env var" +output="$( + RNS_VERBOSE="true" \ + RNS_CWD="${cwd}" \ + RNS_OPTS='--arg customVarBool true ' \ + "${run_nix_shell_sh}" \ + --argstr customVarStr "This is a custom value." \ + 'echo HELLO' 2>&1 1>/dev/null +)" +assert_match "RNS_CWD: .*working-dir" "${output}" "${assert_msg}" +assert_match "RNS_OPTS: --arg customVarBool true" "${output}" "${assert_msg}" +assert_match "RNS_RUN: " "${output}" "${assert_msg}" +assert_match "cwd: .*working-dir" "${output}" "${assert_msg}" +assert_match 'nix_shell_opts: --argstr customVarStr This\\ is\\ a\\ custom\\ value\.' \ + "${output}" "${assert_msg}" +assert_match "script: echo HELLO" "${output}" "${assert_msg}" diff --git a/tests/tools_tests/working_dir_test.sh b/tests/tools_tests/working_dir_test.sh new file mode 100644 index 0000000..2e62dfa --- /dev/null +++ b/tests/tools_tests/working_dir_test.sh @@ -0,0 +1,13 @@ +# Set up a new working directory +cwd="${PWD}/working-dir" +rm -rf "${cwd}" +mkdir -p "${cwd}" +cp shell.nix "${cwd}/" + +assert_msg="set working-directory flag" +output="$( "${run_nix_shell_sh}" --working-directory "${cwd}" 'echo "${PWD}"' )" +assert_equal "${cwd}" "${output}" "${assert_msg}" + +assert_msg="set RNS_CWD env var" +output="$( RNS_CWD="${cwd}" "${run_nix_shell_sh}" 'echo "${PWD}"' )" +assert_equal "${cwd}" "${output}" "${assert_msg}" From 58e30a024262f07a9115e02a2fbadc1c3e288172 Mon Sep 17 00:00:00 2001 From: Chuck Grindel Date: Fri, 8 Dec 2023 12:10:56 -0700 Subject: [PATCH 58/63] Clean up --- tests/tools_tests/BUILD.bazel | 16 ---------------- tests/tools_tests/run_nix_shell_test.bzl | 1 - 2 files changed, 17 deletions(-) diff --git a/tests/tools_tests/BUILD.bazel b/tests/tools_tests/BUILD.bazel index 4b2f8d7..96297b4 100644 --- a/tests/tools_tests/BUILD.bazel +++ b/tests/tools_tests/BUILD.bazel @@ -1,21 +1,5 @@ load(":run_nix_shell_test.bzl", "run_nix_shell_test") -# sh_test( -# name = "run_nix_shell_test", -# srcs = ["run_nix_shell_test.sh"], -# data = [ -# "//:flake_files", -# "//tools:run_nix_shell", -# ], -# # MacOS sandbox fails this test with the following error: -# # sandbox-exec: sandbox_apply: Operation not permitted -# tags = ["no-sandbox"], -# deps = [ -# "@bazel_tools//tools/bash/runfiles", -# "@cgrindel_bazel_starlib//shlib/lib:assertions", -# ], -# ) - run_nix_shell_test(name = "simple_script_test") run_nix_shell_test(name = "multi_line_script_test") diff --git a/tests/tools_tests/run_nix_shell_test.bzl b/tests/tools_tests/run_nix_shell_test.bzl index aafed06..395881d 100644 --- a/tests/tools_tests/run_nix_shell_test.bzl +++ b/tests/tools_tests/run_nix_shell_test.bzl @@ -8,7 +8,6 @@ def run_nix_shell_test(name, test_file = None, **kwargs): srcs = [test_file], ) - # lib_name_target = ":" + lib_name native.sh_test( name = name, srcs = ["@run_nix_shell//tests/tools_tests:run_nix_shell_test_runner.sh"], From 063fe81e4aac6b6dac20a2bd6e0c4e67fbb5ecae Mon Sep 17 00:00:00 2001 From: Chuck Grindel Date: Fri, 8 Dec 2023 12:19:05 -0700 Subject: [PATCH 59/63] Clean up shell.nix --- tests/integration_tests/shell.nix | 20 ++++++++------------ 1 file changed, 8 insertions(+), 12 deletions(-) diff --git a/tests/integration_tests/shell.nix b/tests/integration_tests/shell.nix index fec72ce..22c1194 100644 --- a/tests/integration_tests/shell.nix +++ b/tests/integration_tests/shell.nix @@ -2,18 +2,14 @@ customVarBool ? false, customVarStr ? "default", lock ? builtins.fromJSON (builtins.readFile ./../../flake.lock), - flakeCompat ? ( - fetchTarball { - url = lock.nodes.flake-compat.locked.url or "https://github.com/edolstra/flake-compat/archive/${lock.nodes.flake-compat.locked.rev}.tar.gz"; - sha256 = lock.nodes.flake-compat.locked.narHash; - } - ), - nixpkgs ? ( - fetchTarball { - url = "https://github.com/NixOS/nixpkgs/archive/${lock.nodes.nixpkgs.locked.rev}.tar.gz"; - sha256 = lock.nodes.nixpkgs.locked.narHash; - } - ), + flakeCompat ? fetchTarball { + url = lock.nodes.flake-compat.locked.url or "https://github.com/edolstra/flake-compat/archive/${lock.nodes.flake-compat.locked.rev}.tar.gz"; + sha256 = lock.nodes.flake-compat.locked.narHash; + }, + nixpkgs ? fetchTarball { + url = "https://github.com/NixOS/nixpkgs/archive/${lock.nodes.nixpkgs.locked.rev}.tar.gz"; + sha256 = lock.nodes.nixpkgs.locked.narHash; + }, }: ( # Call flakeCompat with src pointing to the location of the flake.nix. From 3c4f4dad7116280abb6d37a21fc2e45fdb123edb Mon Sep 17 00:00:00 2001 From: Chuck Grindel Date: Fri, 8 Dec 2023 12:38:58 -0700 Subject: [PATCH 60/63] Try to optimize MacOS unit tests --- .bazelrc | 4 ++++ .github/actions/set_up_runner/action.yaml | 7 +++++++ 2 files changed, 11 insertions(+) diff --git a/.bazelrc b/.bazelrc index 8a5e4ca..e861078 100644 --- a/.bazelrc +++ b/.bazelrc @@ -5,6 +5,10 @@ build --enable_bzlmod common:ci --announce_rc test:ci --test_output=errors --test_summary=terse +# MacOS CI Configuration +# ---------------------- +common:macos_ci --jobs=2 + # Remote Cache Authentication # --------------------------- try-import %workspace%/.bazelrc.auth diff --git a/.github/actions/set_up_runner/action.yaml b/.github/actions/set_up_runner/action.yaml index 3ed27e2..a84941c 100644 --- a/.github/actions/set_up_runner/action.yaml +++ b/.github/actions/set_up_runner/action.yaml @@ -17,3 +17,10 @@ runs: cat >>.bazelrc.local <>.bazelrc.local < Date: Fri, 8 Dec 2023 13:05:13 -0700 Subject: [PATCH 61/63] Update README.md --- README.md | 66 +++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 64 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 918ab0d..88be232 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,64 @@ -# run-nix-shell -GitHub action for executing scripts via nix-shell. +# Execute scripts using `nix-shell` + +[![Continuous Integration](https://github.com/tweag/run-nix-shell/actions/workflows/ci.yaml/badge.svg?event=schedule)](https://github.com/tweag/run-nix-shell/actions/workflows/ci.yaml) + +Executes a script or script file using `nix-shell`. + +## Usage + +To use this action, install Nix on your runner and start executing scripts. + +```yaml +name: Example +on: + workflow_dispatch: # allows manual triggering + +jobs: + run_under_nix: + runs-on: ubuntu-latest + steps: + - name: Checkout the repository + uses: actions/checkout@v4 + - uses: DeterminateSystems/nix-installer-action@v9 + with: + github-token: ${{ secrets.GITHUB_TOKEN }} + + - name: Execute script in Nix shell + uses: tweag/run-nix-shell@v0 + with: + run: | + set -o errexit -o nounset -o pipefail + echo "Hello" + + - name: Execute script file + uses: tweag/run-nix-shell@v0 + with: + run: path/to/my/script + + - name: Configure Nix shell before executing script + uses: tweag/run-nix-shell@v0 + with: + options: | + --arg myNixArg true + --argstr anotherNixArg "Hello, World!" + run: | + set -o errexit -o nounset -o pipefail + echo "Hello" + + - name: Execute script in a specific directory + uses: tweag/run-nix-shell@v0 + with: + working-directory: my/working/directory + run: | + set -o errexit -o nounset -o pipefail + echo "${PWD}" +``` + +## Inputs + +| Input | Description | +| ----- | ----------- | +| `run` | The script to be executed using `nix-shell`. This can be the actual script or a path to as cript file. | +| `options` | Any options that you want to pass to `nix-shell`. | +| `working-directory` | This will be the current working direcotry when the script is executed. | +| `verbose` | Enables additional output for debugging this action. | From deabbdbb95bdaeb60a5896548e9e3cb3a8570ad3 Mon Sep 17 00:00:00 2001 From: Chuck Grindel Date: Fri, 8 Dec 2023 13:21:36 -0700 Subject: [PATCH 62/63] Adjust macos CI config --- .bazelrc | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/.bazelrc b/.bazelrc index e861078..6ed7e7b 100644 --- a/.bazelrc +++ b/.bazelrc @@ -7,7 +7,11 @@ test:ci --test_output=errors --test_summary=terse # MacOS CI Configuration # ---------------------- +# The unit tests have a tendency to timeout when executed on the GH macos +# runners. So, we reduce the number of parallel jobs and increase the timeout +# for the tests. common:macos_ci --jobs=2 +common:macos_ci --test_timeout=600 # Remote Cache Authentication # --------------------------- From 51bed9187838a0ac93b2ae2ea6216691a43e5f84 Mon Sep 17 00:00:00 2001 From: Chuck Grindel Date: Fri, 8 Dec 2023 17:00:30 -0700 Subject: [PATCH 63/63] Allow client to specify pure. Default to true. --- action.yaml | 4 ++++ tests/tools_tests/BUILD.bazel | 16 +++++++++------- tests/tools_tests/pure_test.sh | 31 +++++++++++++++++++++++++++++++ tests/tools_tests/verbose_test.sh | 3 +++ tools/run_nix_shell.sh | 17 ++++++++++++++++- 5 files changed, 63 insertions(+), 8 deletions(-) create mode 100644 tests/tools_tests/pure_test.sh diff --git a/action.yaml b/action.yaml index 6ef56ee..140a1a8 100644 --- a/action.yaml +++ b/action.yaml @@ -6,6 +6,9 @@ inputs: type: string required: true description: The path to a file that contains shell code or the actual shell code. + pure: + type: boolean + default: true options: type: string description: Any parameters that are to be passed to nix-shell are specified here. @@ -26,5 +29,6 @@ runs: RNS_CWD: ${{ inputs.working-directory }} RNS_OPTS: ${{ inputs.options }} RNS_RUN: ${{ inputs.run }} + RNS_PURE: ${{ inputs.pure }} RNS_VERBOSE: ${{ inputs.verbose }} run: ${GITHUB_ACTION_PATH}/tools/run_nix_shell.sh diff --git a/tests/tools_tests/BUILD.bazel b/tests/tools_tests/BUILD.bazel index 96297b4..504c356 100644 --- a/tests/tools_tests/BUILD.bazel +++ b/tests/tools_tests/BUILD.bazel @@ -1,17 +1,19 @@ load(":run_nix_shell_test.bzl", "run_nix_shell_test") -run_nix_shell_test(name = "simple_script_test") - -run_nix_shell_test(name = "multi_line_script_test") - -run_nix_shell_test(name = "script_file_test") - run_nix_shell_test(name = "custom_var_bool_test") run_nix_shell_test(name = "custom_var_str_test") +run_nix_shell_test(name = "multi_line_script_test") + +run_nix_shell_test(name = "pure_test") + run_nix_shell_test(name = "rns_opts_test") -run_nix_shell_test(name = "working_dir_test") +run_nix_shell_test(name = "script_file_test") + +run_nix_shell_test(name = "simple_script_test") run_nix_shell_test(name = "verbose_test") + +run_nix_shell_test(name = "working_dir_test") diff --git a/tests/tools_tests/pure_test.sh b/tests/tools_tests/pure_test.sh new file mode 100644 index 0000000..ad5045d --- /dev/null +++ b/tests/tools_tests/pure_test.sh @@ -0,0 +1,31 @@ +assert_msg="pure by default" +output="$( + "${run_nix_shell_sh}" --verbose 'echo "Hello, World!"' 2>&1 1>/dev/null +)" +assert_match "nix-shell .*--pure " "${output}" "${assert_msg}" + +assert_msg="pure flag" +output="$( + "${run_nix_shell_sh}" --verbose --pure 'echo "Hello, World!"' 2>&1 1>/dev/null +)" +assert_match "nix-shell .*--pure " "${output}" "${assert_msg}" + +assert_msg="nopure flag" +output="$( + "${run_nix_shell_sh}" --verbose --nopure 'echo "Hello, World!"' 2>&1 1>/dev/null +)" +assert_no_match "nix-shell .*--pure " "${output}" "${assert_msg}" + +assert_msg="set RNS_PURE to true" +output="$( + RNS_PURE=true \ + "${run_nix_shell_sh}" --verbose 'echo "Hello, World!"' 2>&1 1>/dev/null +)" +assert_match "nix-shell .*--pure " "${output}" "${assert_msg}" + +assert_msg="set RNS_PURE to false" +output="$( + RNS_PURE=false \ + "${run_nix_shell_sh}" --verbose 'echo "Hello, World!"' 2>&1 1>/dev/null +)" +assert_no_match "nix-shell .*--pure " "${output}" "${assert_msg}" diff --git a/tests/tools_tests/verbose_test.sh b/tests/tools_tests/verbose_test.sh index 38a95e2..8360061 100644 --- a/tests/tools_tests/verbose_test.sh +++ b/tests/tools_tests/verbose_test.sh @@ -9,14 +9,17 @@ output="$( RNS_VERBOSE="true" \ RNS_CWD="${cwd}" \ RNS_OPTS='--arg customVarBool true ' \ + RNS_PURE="true" \ "${run_nix_shell_sh}" \ --argstr customVarStr "This is a custom value." \ 'echo HELLO' 2>&1 1>/dev/null )" assert_match "RNS_CWD: .*working-dir" "${output}" "${assert_msg}" assert_match "RNS_OPTS: --arg customVarBool true" "${output}" "${assert_msg}" +assert_match "RNS_PURE: true" "${output}" "${assert_msg}" assert_match "RNS_RUN: " "${output}" "${assert_msg}" assert_match "cwd: .*working-dir" "${output}" "${assert_msg}" assert_match 'nix_shell_opts: --argstr customVarStr This\\ is\\ a\\ custom\\ value\.' \ "${output}" "${assert_msg}" +assert_match "pure: true" "${output}" "${assert_msg}" assert_match "script: echo HELLO" "${output}" "${assert_msg}" diff --git a/tools/run_nix_shell.sh b/tools/run_nix_shell.sh index 2a98660..0ba203a 100755 --- a/tools/run_nix_shell.sh +++ b/tools/run_nix_shell.sh @@ -19,6 +19,7 @@ fail() { cwd="${RNS_CWD:-}" script="${RNS_RUN:-}" +pure="${RNS_PURE:-true}" verbose="${RNS_VERBOSE:-false}" nix_shell_opts=() while (("$#")); do @@ -30,6 +31,14 @@ while (("$#")); do nix_shell_opts+=( "${1}" "${2}" "${3}" ) shift 3 ;; + --pure) + pure="true" + shift 1 + ;; + --nopure) + pure="false" + shift 1 + ;; --working-directory) cwd="${2}" shift 2 @@ -61,8 +70,10 @@ if is_verbose; then RNS_CWD: ${RNS_CWD:-} RNS_OPTS: ${RNS_OPTS:-} RNS_RUN: ${RNS_RUN:-} +RNS_PURE: ${RNS_PURE:-} cwd: ${cwd:-} nix_shell_opts: $( printf "%q " "${nix_shell_opts[@]}" ) +pure: ${pure:-} script: ${script:-} === EOF @@ -72,6 +83,10 @@ fi # MARK - Process Options and Arguments +if [[ "${pure}" == "true" ]]; then + nix_shell_opts+=( --pure ) +fi + # Look for any options passed via the RNS_OPTS environment variable. # Add them to the nix_shell_opts. if [[ -n "${RNS_OPTS:-}" ]]; then @@ -96,7 +111,7 @@ if [[ -n "${cwd:-}" ]]; then cd "${cwd}" fi -cmd=( nix-shell --pure ) +cmd=( nix-shell ) if [[ ${#nix_shell_opts[@]} -gt 0 ]]; then cmd+=( "${nix_shell_opts[@]}" ) fi