diff --git a/devenv.nix b/devenv.nix index 5499b29..7c07e76 100644 --- a/devenv.nix +++ b/devenv.nix @@ -71,6 +71,16 @@ let { match.target = "vault"; action = "deny"; } ]; }; + # Agent-written Nix runs in the builder once its use is approved; `master` is reviewed + builder = { + action = "ask"; + leases = [ "5m" "1h" ]; + rules = [{ match.on_branch = "true"; action = "auto"; }]; + }; + deploy = { + action = "ask"; + leases = [ "5m" ]; + }; # Approvals show whether the file and its recipients match `master`; reading always asks secret = { action = "ask"; @@ -88,7 +98,7 @@ let ssh = "${ssh}"; key = "${state}/broker-key"; known_hosts = "${state}/known_hosts"; - targets = lib.genAttrs [ "web" "router" "vault" ] (_: { + targets = lib.genAttrs [ "box" "web" "router" "vault" ] (_: { hostname = "localhost"; user = "box"; ssh_opts = [ "-p" (toString demo.ports.boxSsh) ]; @@ -230,7 +240,7 @@ in exec = '' set -eu umask 077 - export PATH=${lib.makeBinPath (with pkgs; [ coreutils git rage openssl wireguard-tools ])} + export PATH=${lib.makeBinPath (with pkgs; [ coreutils git rage openssl wireguard-tools jq ])} if [ ! -d ${repositories}/repo.git ]; then for name in dev broker; do @@ -243,6 +253,9 @@ in cp -r ${./nix/demo/repo}/. "$work" chmod -R u+w "$work" mkdir "$work/secrets" + # nixpkgs as this repository locks it, so the builder already has it + jq '{nodes: {root: {inputs: {nixpkgs: "nixpkgs"}}, nixpkgs: .nodes[.nodes.root.inputs.nixpkgs]}, root: "root", version: 7}' \ + ${./flake.lock} > "$work/flake.lock" cat > "$work/secrets.nix" < | Part of the broker | | [Pocket ID](https://pocket-id.org), for approvers to sign in | | `devenv up` (process `pocket-id`) | -| Box VM ([`box.nix`](../nix/demo/box.nix)): `sshd` for the targets | `localhost:2222`, as `box` | `devenv up` (process `box`) | +| Box VM ([`box.nix`](../nix/demo/box.nix)): `sshd` for the targets, and the deploy target | `localhost:2222`, as `box` | `devenv up` (process `box`) | | Agents' VM ([`agent.nix`](../nix/demo/agent.nix)), signed in as `agent` | Your terminal | `demo` | | The repository, seeded from [`repo/`](../nix/demo/repo) | `.devenv/state/slop-demo/git/repo.git` | `git daemon`, one per connection from the agents' VM | | The builder (`slop-broker builder`, from this checkout) | Host | The broker, through its `ssh` | @@ -39,7 +39,8 @@ CID are in [`settings.nix`](../nix/demo/settings.nix). The broker's configuratio [the repository](#the-repository)). - **Approvers:** members of your primary group, through `slop-approve` in the devenv shell, and the Pocket ID user `demo` (in the group `slop-approvers`) through the web app. -- **Targets:** `web`, `router` and `vault` all reach the box VM. +- **Targets:** `box`, `web`, `router` and `vault` all reach the box VM; `box` is also the deploy + node. - **Policy:** - The box queries (`slop status`, `slop logs`, `slop failed`) run without asking. - `slop ssh web` asks, and offers leases of 5 minutes or an hour. @@ -48,6 +49,8 @@ CID are in [`settings.nix`](../nix/demo/settings.nix). The broker's configuratio - `slop ssh vault` is denied. - `slop secret keys` runs without asking; the other secret requests ask, with a 5-minute lease, except reading, which asks every time. + - Using the builder asks, with leases, except for commits already on `master`; deploys ask. + There's no binary cache, so `slop build` says it isn't configured. ## Approving @@ -105,6 +108,27 @@ approvals show the new files as on `master`. To check a value as the demo person rage -d -i .devenv/state/slop-demo/dev-age.key <(git -C .devenv/state/slop-demo/git/repo.git show master:secrets/app.env.age) ``` +### Deploys + +The repository's `flake.nix` has a deploy-rs node, `box`, with one profile, `demo`, deployed as +root: a small app whose activation writes its version to `/run/demo/version`. Its `nixpkgs` is +locked to this repository's, so the builder has it already, and it uses nixpkgs' build of +deploy-rs, so a build takes moments. In `~/repo` on the agents' VM: + +```sh +sed -i 's/version = "1.0"/version = "1.1"/' flake.nix +git switch -c bump && git commit -am "Bump the app" && git push origin bump +slop deploy box ` on the host moves it, standing in for a person mergin - `secrets/app.env.age` holds `DB_USER` and `DB_PASSWORD`; `secrets/wg.key.age` is a WireGuard private key, as a whole file. -Change secrets with `slop secret` (`slop guide` explains it), commit, and push a branch. +`flake.nix` has a deploy-rs node, `box`, with one profile, `demo`: a small app whose activation +writes its version to `/run/demo/version`. + +Change secrets with `slop secret` (`slop guide` explains it), commit, and push a branch. Deploy a +pushed commit with `slop deploy box`. diff --git a/nix/demo/repo/flake.nix b/nix/demo/repo/flake.nix index 8ba2f42..69591b6 100644 --- a/nix/demo/repo/flake.nix +++ b/nix/demo/repo/flake.nix @@ -1,7 +1,73 @@ # The demo's repository, which the agent clones, changes and pushes. `devenv up` seeds it once, -# with `secrets.nix` and the secrets beside this file. +# with `secrets.nix`, the secrets and `flake.lock` beside this file. { description = "slop-broker demo repository"; - outputs = { self }: { }; + inputs.nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable"; + + outputs = { self, nixpkgs }: + let + pkgs = nixpkgs.legacyPackages.x86_64-linux; + + # Change this, push a branch and `slop deploy box`: the approval shows the version change + version = "1.0"; + + app = pkgs.writeTextFile { + name = "demo-app-${version}"; + destination = "/bin/demo-app"; + executable = true; + text = '' + #!${pkgs.runtimeShell} + echo "demo app ${version}" + ''; + }; + + # A profile deploy-rs can activate, as `deploy-rs.lib..activate.custom` makes them, + # with nixpkgs' build of deploy-rs so nothing needs compiling + activatable = base: activate: pkgs.buildEnv { + name = "activatable-${base.name}"; + paths = [ + base + (pkgs.writeTextFile { + name = "deploy-rs-activate"; + destination = "/deploy-rs-activate"; + executable = true; + text = '' + #!${pkgs.runtimeShell} + set -eu + if [ "''${BOOT:-}" = 1 ]; then + echo "nothing to stage for boot" + exit 0 + fi + ${activate} + ''; + }) + (pkgs.writeTextFile { + name = "activate-rs"; + destination = "/activate-rs"; + executable = true; + text = '' + #!${pkgs.runtimeShell} + exec ${pkgs.deploy-rs}/bin/activate "$@" + ''; + }) + ]; + }; + in + { + packages.x86_64-linux.default = app; + + # The broker reaches nodes by its own SSH targets, so `hostname` isn't used + deploy.nodes.box = { + hostname = "localhost"; + profiles.demo = { + user = "root"; + path = activatable app '' + mkdir -p /run/demo + echo ${version} > /run/demo/version + echo "demo app ${version} is active" + ''; + }; + }; + }; }