-
Notifications
You must be signed in to change notification settings - Fork 276
feat: Add a Nix Flake with a devShell, NixOS module, and home-manager module to configure Shell Plugins #435
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
Merged
Merged
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
f230b4e
feat: add initial flake.nix
mrjones2014 acc4868
fix: Move nixos and home-manager modules outside of `flake-utils.each…
mrjones2014 92568af
chore: Add comment explanation
mrjones2014 2d9bb29
chore: Refactor to make it easier to validate
mrjones2014 df780b6
feat(ci): Add CI job to check Nix flake
mrjones2014 5888a41
chore: Rename properties
mrjones2014 7431871
chore(ci): Add CI job to update flake dependencies
mrjones2014 ec0377a
chore: Add comment explanation
mrjones2014 5ece816
fix: Add OP_PLUGINS_SOURCED=1 environment variable
mrjones2014 13a3b13
fix: use function from lib instead of builtins
mrjones2014 e909e4b
Merge branch 'mrj/nix-flake-overlay' of github.com:1Password/shell-pl…
mrjones2014 39d777c
chore: Remove flake.lock and update update-flake-dependencies.yml as …
mrjones2014 01d503a
fix: create the branch on the remote before trying to commit to it vi…
mrjones2014 8ac3f45
fix: add --body argument
mrjones2014 4cd313c
chore: Update flake.lock
mrjones2014 3c5ef63
chore: Remove CI trigger that was added as a test
mrjones2014 b2743a2
fix: only create flake.lock PR if there are actually changes
mrjones2014 e751aa5
feat: Validate specified plugins against the list of supported plugins
mrjones2014 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| name: Check Nix flake | ||
| on: | ||
| pull_request: | ||
| push: | ||
| branches: | ||
| - main | ||
| jobs: | ||
| check-flake: | ||
| runs-on: ubuntu-latest | ||
| steps: | ||
| - uses: actions/checkout@v4 | ||
| - uses: DeterminateSystems/nix-installer-action@main | ||
| - uses: DeterminateSystems/magic-nix-cache-action@main | ||
| - name: Check Nix flake inputs | ||
| uses: DeterminateSystems/flake-checker-action@v5 | ||
| with: | ||
| fail-mode: true | ||
| send-statistics: false | ||
| - name: Run nix flake check | ||
| run: nix flake check |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| # CI job to periodically (once a week) update flake.lock | ||
| name: Update flake dependencies | ||
|
|
||
| on: | ||
| schedule: | ||
| - cron: '0 16 * * 5' | ||
| workflow_dispatch: # for allowing manual triggers of the workflow | ||
|
|
||
| jobs: | ||
| update-dependencies: | ||
| runs-on: ubuntu-latest | ||
| steps: | ||
| - uses: actions/checkout@v4 | ||
| - uses: DeterminateSystems/nix-installer-action@main | ||
| - uses: DeterminateSystems/magic-nix-cache-action@main | ||
| - name: update flake.lock | ||
| run: nix flake update | ||
| - name: Create signed commit with flake.lock changes | ||
| env: | ||
| GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | ||
| FILE_TO_COMMIT: flake.lock | ||
| COMMIT_BRANCH: automation/update-flake-dependencies | ||
| COMMIT_MESSAGE: "chore(nix): Update Flake dependencies" | ||
| run: | | ||
| # make sure something actually changed first, if not, no updates required | ||
| if [[ `git status --porcelain` ]]; then | ||
| # create the branch on the remote | ||
| git branch "$COMMIT_BRANCH" | ||
| git push -u origin "$COMMIT_BRANCH" | ||
| # commit via the GitHub API so we get automatic commit signing | ||
| gh api --method PUT /repos/1Password/shell-plugins/contents/$FILE_TO_COMMIT \ | ||
| --field message="$COMMIT_MESSAGE" \ | ||
| --field content=@<(base64 -i $FILE_TO_COMMIT) \ | ||
| --field branch="$COMMIT_BRANCH" \ | ||
| --field sha="$(git rev-parse $COMMIT_BRANCH:$FILE_TO_COMMIT)" | ||
| gh pr create --title "[automation]: Update Flake dependencies" --body "This is an automated PR to update \`flake.lock\`" --reviewer mrjones2014 --base main --head $COMMIT_BRANCH | ||
| fi | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -19,3 +19,7 @@ plugins/registry.json | |
|
|
||
| # 1Password | ||
| .op | ||
|
|
||
| # direnv | ||
| .direnv | ||
| .envrc | ||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| { | ||
| inputs = { | ||
| nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable"; | ||
| flake-utils = { url = "github:numtide/flake-utils"; }; | ||
| }; | ||
| outputs = { nixpkgs, flake-utils, ... }: | ||
| (flake-utils.lib.eachDefaultSystem (system: | ||
| let pkgs = nixpkgs.legacyPackages.${system}; | ||
| in { | ||
| devShells.default = pkgs.mkShell { | ||
| name = "Shell with Go toolchain"; | ||
| packages = with pkgs; [ go gopls ]; | ||
| }; | ||
| })) // { | ||
| nixosModules.default = import ./nix/nixos.nix; | ||
| hmModules.default = import ./nix/home-manager.nix; | ||
| }; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| { pkgs, lib, config, ... }: | ||
| import ./shell-plugins.nix { | ||
| inherit pkgs; | ||
| inherit lib; | ||
| inherit config; | ||
| is-home-manager = true; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| { pkgs, lib, config, ... }: | ||
| import ./shell-plugins.nix { | ||
| inherit pkgs; | ||
| inherit lib; | ||
| inherit config; | ||
| is-home-manager = false; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,85 @@ | ||
| { pkgs, lib, config, is-home-manager, ... }: | ||
| with lib; | ||
|
mrjones2014 marked this conversation as resolved.
|
||
| let | ||
| cfg = config.programs._1password-shell-plugins; | ||
|
|
||
| supported_plugins = splitString "\n" (lib.readFile "${ | ||
| # get the list of supported plugin executable names | ||
| pkgs.runCommand "op-plugin-list" { } | ||
| # 1Password CLI tries to create the config directory automatically, so set a temp XDG_CONFIG_HOME | ||
| # since we don't actually need it for this | ||
| "mkdir $out && XDG_CONFIG_HOME=$out ${pkgs._1password}/bin/op plugin list | cut -d ' ' -f1 | tail -n +2 > $out/plugins.txt" | ||
| }/plugins.txt"); | ||
| getExeName = package: | ||
| # NOTE: SAFETY: This is okay because the `packages` list is also referred | ||
| # to below as `home.packages = packages;` or `environment.systemPackages = packages;` | ||
| # depending on if it's using `home-manager` or not; this means that Nix can still | ||
| # compute the dependency tree, even though we're discarding string context here, | ||
| # since the packages are still referred to below without discarding string context. | ||
| strings.unsafeDiscardStringContext (baseNameOf (getExe package)); | ||
| in { | ||
| options = { | ||
|
mrjones2014 marked this conversation as resolved.
|
||
| programs._1password-shell-plugins = { | ||
| enable = mkEnableOption "1Password Shell Plugins"; | ||
|
mrjones2014 marked this conversation as resolved.
|
||
| plugins = mkOption { | ||
| type = types.listOf types.package; | ||
| default = [ ]; | ||
| example = literalExpression '' | ||
| with pkgs; [ | ||
| gh | ||
| awscli2 | ||
| cachix | ||
| ] | ||
| ''; | ||
| description = | ||
| "CLI Packages to enable 1Password Shell Plugins for; ensure that a Shell Plugin exists by checking the docs: https://developer.1password.com/docs/cli/shell-plugins/"; | ||
| # this is a bit of a hack to do option validation; | ||
| # ensure that the list of packages include only packages | ||
| # for which the executable has a supported 1Password Shell Plugin | ||
| apply = package_list: | ||
| map (package: | ||
| if (elem (getExeName package) supported_plugins) then | ||
| package | ||
| else | ||
| abort "${ | ||
| getExeName package | ||
| } is not a valid 1Password Shell Plugin. A list of supported plugins can be found by running `op plugin list` or at: https://developer.1password.com/docs/cli/shell-plugins/") | ||
| package_list; | ||
| }; | ||
| }; | ||
| }; | ||
|
|
||
| config = let | ||
|
mrjones2014 marked this conversation as resolved.
|
||
| # executable names as strings, e.g. `pkgs.gh` => `"gh"`, `pkgs.awscli2` => `"aws"` | ||
| pkg-exe-names = map getExeName cfg.plugins; | ||
| # Explanation: | ||
| # Map over `cfg.plugins` (the value of the `plugins` option provided by the user) | ||
| # and for each package specified, get the executable name, then create a shell alias | ||
| # of the form: | ||
| # `alias {pkg}="op plugin run -- {pkg}"` | ||
| # where `{pkg}` is the executable name of the package | ||
| aliases = listToAttrs (map (package: { | ||
| name = package; | ||
| value = "op plugin run -- ${package}"; | ||
| }) pkg-exe-names); | ||
|
mrjones2014 marked this conversation as resolved.
|
||
| packages = [ pkgs._1password ] ++ cfg.plugins; | ||
| in mkIf cfg.enable (mkMerge [ | ||
|
mrjones2014 marked this conversation as resolved.
|
||
| ({ | ||
| programs = { | ||
| bash.shellAliases = aliases; | ||
| zsh.shellAliases = aliases; | ||
| fish.shellAliases = aliases; | ||
| }; | ||
| } // optionalAttrs is-home-manager { | ||
| home = { | ||
| inherit packages; | ||
| sessionVariables = { OP_PLUGINS_SOURCED = "1"; }; | ||
| }; | ||
| } // optionalAttrs (!is-home-manager) { | ||
| environment = { | ||
| systemPackages = packages; | ||
| variables = { OP_PLUGINS_SOURCED = "1"; }; | ||
| }; | ||
| }) | ||
| ]); | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.