Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
63 commits
Select commit Hold shift + click to select a range
2e7982a
Initial files
cgrindel Dec 6, 2023
fb28804
Reworked to accept --arg*
cgrindel Dec 6, 2023
97d3688
Added failing test for run_nix_shell
cgrindel Dec 6, 2023
1e08db3
Initial flake.nix and flake.lock
cgrindel Dec 6, 2023
16c28d5
Add nixpkgs.nix
cgrindel Dec 6, 2023
423b0fc
Switch to using bazelisk.
cgrindel Dec 6, 2023
c6ef3e7
Add envrc
cgrindel Dec 6, 2023
57efc0b
Add rules_nixpkgs
cgrindel Dec 6, 2023
723e679
Update nixpkgs to more recent version
cgrindel Dec 7, 2023
1af9c11
The test ran successfully.
cgrindel Dec 7, 2023
fe120df
Clean up
cgrindel Dec 7, 2023
69bd851
Initial tests are green
cgrindel Dec 7, 2023
f6c0a8b
Processing args from command line
cgrindel Dec 7, 2023
fb5c1d4
Clean up
cgrindel Dec 7, 2023
7c5f985
Remove bazeliskrc
cgrindel Dec 7, 2023
eba9418
Update bazelrc to load auth and local
cgrindel Dec 8, 2023
c70a672
Remove rules_nixpkgs. Not needed.
cgrindel Dec 8, 2023
335c53a
First pass at CI
cgrindel Dec 8, 2023
c52bdeb
Add default.nix.
cgrindel Dec 8, 2023
0bfe1d2
Add CI Bazel config
cgrindel Dec 8, 2023
882637c
Fix silly mistake
cgrindel Dec 8, 2023
73562e2
Add integration test
cgrindel Dec 8, 2023
21439aa
Specify GITHUB_ACTION_PATH
cgrindel Dec 8, 2023
34de990
Add debug
cgrindel Dec 8, 2023
bc57352
Remove slash
cgrindel Dec 8, 2023
bcd3907
Add slash back
cgrindel Dec 8, 2023
3b986e2
Fix path to script
cgrindel Dec 8, 2023
b8f0a15
Clean up
cgrindel Dec 8, 2023
7714c1b
Debug code
cgrindel Dec 8, 2023
7c7ffbf
Point to nixpkgs.nix
cgrindel Dec 8, 2023
4a198ff
Add shell.nix to root so that we can execute nix-shell.
cgrindel Dec 8, 2023
239c1d8
Refactor integration tests to their own jobs
cgrindel Dec 8, 2023
690cc48
Add shell attribute
cgrindel Dec 8, 2023
145f7d4
Move shell.nix to integration tests directory
cgrindel Dec 8, 2023
2725e6d
Incorporate variables into shell.nix
cgrindel Dec 8, 2023
44eeb73
Refactored lock to be an arg
cgrindel Dec 8, 2023
6e37a44
Update integration test
cgrindel Dec 8, 2023
f51ad99
Add support for working-directory
cgrindel Dec 8, 2023
78072eb
Read output from correct directory
cgrindel Dec 8, 2023
f425fab
Add verbose to the script
cgrindel Dec 8, 2023
211b9b3
Enable verbose in the integration test
cgrindel Dec 8, 2023
51c8533
Disable verbose
cgrindel Dec 8, 2023
c48a789
Enable verbose
cgrindel Dec 8, 2023
0765496
Ensure output file for integration test does not exist.
cgrindel Dec 8, 2023
8e5e7a6
Debug code
cgrindel Dec 8, 2023
0c1819d
No need to delete the integration_test.out file
cgrindel Dec 8, 2023
d5f20f1
Enable flakes
cgrindel Dec 8, 2023
5e1531b
Try using DeterminateSystems installer
cgrindel Dec 8, 2023
e1b4c09
Fix typo
cgrindel Dec 8, 2023
9711e74
Put back original shell.nix
cgrindel Dec 8, 2023
0111012
Add actual command-line invocation
cgrindel Dec 8, 2023
33694d7
Clean up
cgrindel Dec 8, 2023
c199b5f
Fix typo
cgrindel Dec 8, 2023
5642b19
Add magic cache
cgrindel Dec 8, 2023
2d009b8
Refactor shell.nix
cgrindel Dec 8, 2023
9b5036c
Start refactor to separate tests.
cgrindel Dec 8, 2023
6097798
Created separate tests
cgrindel Dec 8, 2023
58e30a0
Clean up
cgrindel Dec 8, 2023
063fe81
Clean up shell.nix
cgrindel Dec 8, 2023
3c4f4da
Try to optimize MacOS unit tests
cgrindel Dec 8, 2023
5823301
Update README.md
cgrindel Dec 8, 2023
deabbdb
Adjust macos CI config
cgrindel Dec 8, 2023
51bed91
Allow client to specify pure. Default to true.
cgrindel Dec 9, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions .bazelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
build --enable_bzlmod

# CI Configuration
# ----------------
common:ci --announce_rc
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
# ---------------------------
try-import %workspace%/.bazelrc.auth

# User Configuration
# ------------------
try-import %workspace%/.bazelrc.local
1 change: 1 addition & 0 deletions .envrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
use flake
26 changes: 26 additions & 0 deletions .github/actions/set_up_runner/action.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
name: Set up GitHub runner

inputs:
github_token:
type: string

runs:
using: composite
steps:
- uses: DeterminateSystems/nix-installer-action@v9
with:
github-token: ${{ inputs.github_token }}
- uses: DeterminateSystems/magic-nix-cache-action@v2
- name: Configure
shell: bash
run: |
cat >>.bazelrc.local <<EOF
common --config=ci
EOF
- name: Configure MacOS
if: ${{ runner.os == 'macOS' }}
shell: bash
run: |
cat >>.bazelrc.local <<EOF
common --config=macos_ci
EOF
80 changes: 80 additions & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
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: Unit Tests
strategy:
fail-fast: false
matrix:
os: [ubuntu-latest, macos-latest]
runs-on: ${{ matrix.os }}
steps:
- 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 //...

integration_tests:
name: Integration Tests
strategy:
fail-fast: false
matrix:
os: [ubuntu-latest, macos-latest]
runs-on: ${{ matrix.os }}
steps:
- 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:
verbose: true
working-directory: ./tests/integration_tests
options: |
--arg customVarBool 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="$(<./tests/integration_tests/integration_test.out)"
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
needs:
- unit_tests
- integration_tests
if: ${{ always() }}
steps:
- uses: cgrindel/gha_join_jobs@794a2d117251f22607f1aab937d3fd3eaaf9a2f5 # v1
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
bazel-*

.direnv
10 changes: 10 additions & 0 deletions BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# exports_files(["flake.lock"])

filegroup(
name = "flake_files",
srcs = [
"flake.lock",
"flake.nix",
],
visibility = ["//:__subpackages__"],
)
15 changes: 15 additions & 0 deletions MODULE.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
module(
name = "run_nix_shell",
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,
)
66 changes: 64 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -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. |
1 change: 1 addition & 0 deletions WORKSPACE
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Intentionally blank; using bzlmod
2 changes: 2 additions & 0 deletions WORKSPACE.bzlmod
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Intentionally blank
# This file exists to force bzlmod into a strict mode.
34 changes: 34 additions & 0 deletions action.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
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.
pure:
type: boolean
default: true
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: .
verbose:
type: boolean
description: Enable debug output written to stderr.
default: false

runs:
using: composite
steps:
- shell: bash
env:
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
78 changes: 78 additions & 0 deletions flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

33 changes: 33 additions & 0 deletions flake.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{
inputs = {
nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
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;
};
};
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 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++";
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,
packages = [ bazel_6 bazel-buildtools cacert gcc nix git openssh ];
};
});
}
8 changes: 8 additions & 0 deletions nixpkgs.nix
Original file line number Diff line number Diff line change
@@ -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
4 changes: 4 additions & 0 deletions tests/integration_tests/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Integration Tests

This directory contains the files needed to execute the integration tests as configured in the CI
GitHub workflow.
Loading