Skip to content
Cachix

Deploying to agents

Start by getting your agent running.

Generate an activation token to authenticate cachix deploy activate. Running that command starts a deployment for the agents listed in your deployment specification.

  1. In your Cachix Deploy workspace, click Start a Deployment to open the token form.
  2. Enter a description for the token, such as “GitHub Actions CD”.
  3. Click Generate.
  4. Copy and save the token.

Depending on what are you deploying to an agent, here are some examples:

The following deploys NixOS to the agent named myagent in a file named flake.nix:

{
inputs = {
nixpkgs.url = "github:NixOS/nixpkgs/nixos-22.05";
cachix-deploy-flake.url = "github:cachix/cachix-deploy-flake";
flake-utils.url = "github:numtide/flake-utils";
};
outputs = { self, nixpkgs, flake-utils, cachix-deploy-flake }:
flake-utils.lib.eachDefaultSystem (
system: {
defaultPackage = let
pkgs = import nixpkgs { inherit system; };
cachix-deploy-lib = cachix-deploy-flake.lib pkgs;
in
cachix-deploy-lib.spec {
agents = {
myagent = cachix-deploy-lib.nixos {
fileSystems."/" = { device = "/dev/disk/by-label/nixos"; };
boot.loader.grub.devices = [ "/dev/sda" ];
boot.loader.grub.enable = true;
networking.hostName = "myagent";
};
};
};
}
);
}

To fully grasp the JSON specification see the reference.

The following deploys nix-darwin to the agent named myagent in a file named flake.nix:

{
inputs = {
nixpkgs.url = "github:NixOS/nixpkgs/nixos-22.05";
flake-utils.url = "github:numtide/flake-utils";
cachix-deploy-flake.url = "github:cachix/cachix-deploy-flake";
cachix-deploy-flake.inputs.darwin.follows = "darwin";
darwin.url = "github:LnL7/nix-darwin";
darwin.inputs.nixpkgs.follows = "nixpkgs";
};
outputs = { self, flake-utils, darwin, nixpkgs, cachix-deploy-flake }:
flake-utils.lib.eachDefaultSystem (
system: {
defaultPackage = let
pkgs = import nixpkgs { inherit system; };
cachix-deploy-lib = cachix-deploy-flake.lib pkgs;
in
cachix-deploy-lib.spec {
agents = {
myagent = cachix-deploy-lib.darwin (
{ pkgs, ... }:
{
networking.hostName = "myagent";
services.cachix-agent.enable = true;
# Auto upgrade nix package and the daemon service.
services.nix-daemon.enable = true;
nix.package = pkgs.nix;
}
);
};
};
}
);
}
{
inputs = {
nixpkgs.url = "github:NixOS/nixpkgs/nixos-22.05";
flake-utils.url = "github:numtide/flake-utils";
cachix-deploy-flake.url = "github:cachix/cachix-deploy-flake";
};
outputs = { self, flake-utils, nixpkgs, cachix-deploy-flake }:
flake-utils.lib.eachDefaultSystem (
system: {
defaultPackage = let
pkgs = import nixpkgs { inherit system; };
cachix-deploy-lib = cachix-deploy-flake.lib pkgs;
in
cachix-deploy-lib.spec {
agents = {
myagent = pkgs.git;
};
};
}
);
}

The following deploys a standalone Home Manager to the agent named myagent in a file named flake.nix:

{
inputs = {
nixpkgs.url = "github:NixOS/nixpkgs/nixos-22.05";
flake-utils.url = "github:numtide/flake-utils";
cachix-deploy-flake.url = "github:cachix/cachix-deploy-flake";
cachix-deploy-flake.inputs.home-manager.follows = "home-manager";
home-manager.url = "github:nix-community/home-manager";
home-manager.inputs.nixpkgs.follows = "nixpkgs";
};
outputs = { self, flake-utils, home-manager, nixpkgs, cachix-deploy-flake }:
flake-utils.lib.eachDefaultSystem (
system: {
defaultPackage = let
pkgs = nixpkgs.legacyPackages."${system}";
cachix-deploy-lib = cachix-deploy-flake.lib pkgs;
in
cachix-deploy-lib.spec {
agents = {
myagent = cachix-deploy-lib.homeManager { } (
{ pkgs, ... }:
{
home.username = "jdoe";
home.homeDirectory = "/home/jdoe";
home.stateVersion = "22.05";
services.cachix-agent = {
enable = true;
name = "myagent";
};
}
);
};
};
}
);
}

Replace MY_CACHE with the name of the binary cache you created. You’ll need two tokens:

  • you have a write token to replace CACHE-TOKEN
  • previously generated token to replace ACTIVATE-TOKEN.

The following snippet will build your machine, push binaries to your cache and deploy your agent:

export CACHIX_ACTIVATE_TOKEN=ACTIVATE-TOKEN
export CACHIX_AUTH_TOKEN=CACHE-TOKEN
spec=$(nix build --print-out-paths)
cachix push MY_CACHE $spec
cachix deploy activate $spec