Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

feat: custom devshell option #333

Merged
merged 3 commits into from
Sep 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,6 @@ jobs:
- name: Install Nix
uses: cachix/install-nix-action@v27
with:
nix_path: nixpkgs=channel:release-21.05
extra_nix_config: |
access-tokens = github.com=${{ secrets.GITHUB_TOKEN }}
- name: Test Basic Bash Shell with Local Flake
Expand Down Expand Up @@ -146,6 +145,24 @@ jobs:
pwd
figlet "I'm from the devshell, baby."

test-flakes-from-devshell-with-custom-shell:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Install Nix
uses: cachix/install-nix-action@v27
with:
extra_nix_config: |
access-tokens = github.com=${{ secrets.GITHUB_TOKEN }}
- name: Test Bash Shell with Local and External Flakes
uses: ./
with:
flakes-from-devshell: true
custom-devshell: ci
script: |
pwd
ponysay "I'm from the CI devshell, baby."

test-flakes-from-devshell-in-working-directory:
runs-on: ubuntu-latest
steps:
Expand Down
76 changes: 40 additions & 36 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,49 +9,50 @@ Run any command you like in a deterministic [Nix](https://nixos.org/nix/) shell
Create `.github/workflows/test.yml` in your repo with the following contents:

```yaml
name: "Test"
name: 'Test'
on:
pull_request:
push:
jobs:
tests:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: cachix/install-nix-action@v18
with:
nix_path: nixpkgs=channel:nixos-unstable
- uses: workflow/nix-shell-action@v3
with:
packages: hello,docker
script: |
hello
docker --help
- uses: actions/checkout@v3
- uses: cachix/install-nix-action@v18
with:
nix_path: nixpkgs=channel:nixos-unstable
- uses: workflow/nix-shell-action@v3
with:
packages: hello,docker
script: |
hello
docker --help
```

You can also pass in environment variables:

```yaml
- uses: workflow/nix-shell-action@v3
env:
TRANSFORMER: bumblecat
with:
packages: hello,docker
script: |
hello $TRANSFORMER
docker --help
- uses: workflow/nix-shell-action@v3
env:
TRANSFORMER: bumblecat
with:
packages: hello,docker
script: |
hello $TRANSFORMER
docker --help
```

For now, this action implicitly depends on having [Nix] installed and set up correctly, such as through the [install-nix-action] demonstrated in the examples above.

See also [cachix-action](https://github.com/cachix/cachix-action) for a simple binary cache setup to speed up your builds and share binaries with developers.

## Usage with Flakes

Instead of specifying packages, you can use `flakes` to specify fully qualified flakes to be available in your script.
This can be used for both local flakes in a `flake.nix` in your repo, as well as external flakes.

```yaml
name: "Test"
name: 'Test'
on:
pull_request:
push:
Expand All @@ -76,10 +77,11 @@ jobs:
```

## Flakes from devShell

Instead of specifying `flakes`, you can also tell this action to re-use the `buildInputs` from your `devShell` defined in a `flake.nix`, and automatically make these available to the script:

```yaml
name: "Test with Flakes from DevShell"
name: 'Test with Flakes from DevShell'
on:
pull_request:
push:
Expand All @@ -103,41 +105,43 @@ jobs:

## Options `with: ...`

- `interpreter`: Interpreter to use in the nix shell shebang, defaults to `bash`. (This is passed to `nix run -c`, used to be `-i` in a nix shell shebang)
- `interpreter`: Interpreter to use in the nix shell shebang, defaults to `bash`. (This is passed to `nix run -c`, used to be `-i` in a nix shell shebang)

- `packages`: Comma-separated list of packages to pre-install in your shell. Cannot be used together with the `flakes` option.

- `flakes`: Comma-separated list of fully qualified flakes to pre-install in your shell. Use either `packages` or `flakes`. Cannot be used together with the `packages` option.

- `flakes-from-devshell`: If true, supply flakes from a `devShell` provided in your repo's `flake.nix`. You cannot currently combined this with the `flakes` nor `packages` options.

- `custom-devshell`: Specify a custom `devShell` to use. This can be useful if you have a `devShell` that is not named `devShell` in your `flake.nix`. You cannot currently combined this with the `flakes` nor `packages` options.

- `script`: The actual script to execute in your shell. Will be passed to the `interpreter`, which defaults to `bash`

- `working-directory`: Execute the script inside the specified working directory instead of the repository root. Example: `path/to/dir`

## FAQ: Passing a Github Token against Rate Limits

```yaml
name: "Test"
name: 'Test'
on:
pull_request:
push:
jobs:
tests:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: cachix/install-nix-action@v18
with:
nix_path: nixpkgs=channel:nixos-unstable
extra_nix_config: |
access-tokens = github.com=${{ secrets.GITHUB_TOKEN }}
- uses: workflow/nix-shell-action@v3
with:
packages: hello,docker
script: |
hello
docker --help
- uses: actions/checkout@v3
- uses: cachix/install-nix-action@v18
with:
nix_path: nixpkgs=channel:nixos-unstable
extra_nix_config: |
access-tokens = github.com=${{ secrets.GITHUB_TOKEN }}
- uses: workflow/nix-shell-action@v3
with:
packages: hello,docker
script: |
hello
docker --help
```

---
Expand All @@ -146,5 +150,5 @@ jobs:

See https://github.com/actions/typescript-action

[Nix]: https://nixos.org/nix/
[nix]: https://nixos.org/nix/
[install-nix-action]: https://github.com/marketplace/actions/install-nix
4 changes: 4 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ inputs:
required: false
description: 'If set to true, uses devShell instead.'
default: false
custom-devshell:
required: false
description: 'If set, uses a customly named devShell from the current flake'
default: ''
script:
required: true
description: 'The actual script to execute in the shell'
Expand Down
3 changes: 2 additions & 1 deletion dist/index.js

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

2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

11 changes: 8 additions & 3 deletions flake.nix
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
{
description = "A very basic flake";

outputs = { self, nixpkgs }: {

outputs = {
self,
nixpkgs,
}: {
packages.x86_64-linux.hello = nixpkgs.legacyPackages.x86_64-linux.hello;

packages.x86_64-linux.default = self.packages.x86_64-linux.hello;

devShells.x86_64-linux.default = nixpkgs.legacyPackages.x86_64-linux.mkShell {
buildInputs = [ nixpkgs.legacyPackages.x86_64-linux.figlet ];
buildInputs = [nixpkgs.legacyPackages.x86_64-linux.figlet];
};

ci = nixpkgs.legacyPackages.x86_64-linux.mkShell {
buildInputs = [nixpkgs.legacyPackages.x86_64-linux.ponysay];
};
};
}
7 changes: 6 additions & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ function run(): void {
const flakesFromDevshell: boolean = core.getBooleanInput(
'flakes-from-devshell'
)
const customDevshell: string = core.getInput('custom-devshell')
const script: string = core.getInput('script')
const workingDirectory: string = core.getInput('working-directory')

Expand All @@ -33,7 +34,11 @@ function run(): void {
.map(pkg => `nixpkgs#${pkg.trim()}`)
.join(' ')

const nixCommand = flakesFromDevshell ? 'develop' : 'shell'
const nixCommand = flakesFromDevshell
? customDevshell
? `develop .#${customDevshell}`
: 'develop'
: 'shell'

const nixWrapper = `
set -euo pipefail
Expand Down