Differences
This shows you the differences between two versions of the page.
| infrastructure:playbook:bitwarden-sops-nix [2026/01/08 09:09] – created diamond | infrastructure:playbook:bitwarden-sops-nix [2026/01/08 09:12] (current) – moved from nixos machines page diamond | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| # Setting up Bitwarden and SOPS for a NixOS Machine | # Setting up Bitwarden and SOPS for a NixOS Machine | ||
| + | If you're here, chances are you're already added into the administration group for Bitwarden users. If not, please reach out to an existing administrator first to be added before continuing. | ||
| + | |||
| + | Before continuing, **it is strongly advised that you have [Nix][nix] and [Direnv][direnv] installed**. This will allow you to store your Bitwarden secrets much easier. | ||
| + | |||
| + | #### Preparing the Bitwarden CLI | ||
| + | |||
| + | First, set the `BITWARDENCLI_APPDATA_DIR` environment variable to prevent the CLI from using or overriding your personal Bitwarden configuration. It is strongly recommended to set this in `.envrc` so you don't forget: | ||
| + | |||
| + | ```sh | ||
| + | $ cat .envrc | ||
| + | export BITWARDENCLI_APPDATA_DIR=" | ||
| + | ... | ||
| + | ``` | ||
| + | |||
| + | Then, you'll need to obtain your Bitwarden API key. For this, follow the [Bitwarden official documentation](https:// | ||
| + | |||
| + | ```sh | ||
| + | $ cat .envrc | ||
| + | export BW_CLIENTID=" | ||
| + | export BW_CLIENTSECRET=" | ||
| + | ``` | ||
| + | |||
| + | Now, proceed to run `bw login --apikey` to login using the exported environment variables, then `bw unlock` to unlock the actual vault. This will prompt you for your master password, so enter that. | ||
| + | |||
| + | Afterwards, you should see an output similar to this: | ||
| + | |||
| + | ``` | ||
| + | $ bw unlock | ||
| + | ? Master password: [hidden] | ||
| + | Your vault is now unlocked! | ||
| + | |||
| + | To unlock your vault, set your session key to the `BW_SESSION` environment variable. ex: | ||
| + | $ export BW_SESSION="< | ||
| + | > $env: | ||
| + | |||
| + | You can also pass the session key to any command with the `--session` option. ex: | ||
| + | $ bw list items --session < | ||
| + | ``` | ||
| + | |||
| + | Simply copy-paste the `export BW_SESSION=...` line into the terminal and run that. You should now be able to run `bw sync`, `bw list`, etc. | ||
| + | |||
| + | #### Adding a Secret | ||
| + | |||
| + | Head to the Bitwarden web app or extension, then navigate to the **Server Credentials/ | ||
| + | |||
| + | To add a secret to a machine, open the corresponding item, then add a new hidden field with the name being the SOPS path you want to store the secret at relative to `/ | ||
| + | |||
| + | For example, do add a secret at `/ | ||
| + | |||
| + | #### Onboard the Machine to SOPS | ||
| + | |||
| + | This step only needs to be done once per machine. To validate that a machine is ready for SOPS, ensure it has the `sops.*` options in its `configuration.nix`. | ||
| + | |||
| + | If not, start by referring to [sops-nix' | ||
| + | |||
| + | 1. Grab the machine' | ||
| + | 2. Add it to `vars.nix` under `< | ||
| + | 3. Find the Bitwarden secret ID. There are 2 ways to do this: | ||
| + | - Using `just get-bitwarden-secret-id < | ||
| + | - Using the `& | ||
| + | 4. Add it to `vars.nix` under `< | ||
| + | |||
| + | Then, add the boilerplate snippet to the machine' | ||
| + | |||
| + | ```nix | ||
| + | { | ||
| + | sops = { | ||
| + | defaultSopsFile = ./ | ||
| + | age.sshKeyPaths = [ "/ | ||
| + | }; | ||
| + | } | ||
| + | ``` | ||
| + | |||
| + | #### Synchronizing Secrets | ||
| + | |||
| + | First, make sure that the local Bitwarden vault is up to date by running `bw sync`. | ||
| + | |||
| + | Then, run `just sync-bitwarden-secrets < | ||
| + | |||
| + | Having more than just the host's recipient key is not recommended. Instead, prefer regenerating the secret file from source Bitwarden if needed. This way, the secrets are always up to date with Bitwarden. | ||
| + | |||
| + | #### Using the Secrets | ||
| + | |||
| + | You may use the secrets in your machine like any other [sops-nix][sops-nix] secrets. For example: | ||
| + | |||
| + | ```nix | ||
| + | sops.secrets." | ||
| + | owner = " | ||
| + | }; | ||
| + | ``` | ||
| + | |||
| + | This will place the secret at `/ | ||
| + | |||
| + | Deploy the machine using `just deploy < | ||
| + | |||
| + | [nix]: https:// | ||
| + | [direnv]: https:// | ||
| + | [sops-nix]: https:// | ||