Staging environment
Staging environments are a good protection against bad things happening to your production server.
1. Declare staging server in flake.nix
You can define staging and production environments in the same Nix flake.
In your flake.nix
, you will find a place where NixOS configurations are defined.
{
nixosConfigurations."my-app" = nixpkgs.lib.nixosSystem {
inherit system;
specialArgs = attrs // {
environment = "production";
};
modules = [
# Overlays-module makes "pkgs.unstable" available in configuration.nix
({ config, pkgs, ... }: {
nixpkgs.overlays = [ overlay-unstable ];
})
./nixos/configuration.nix
];
};
}
You can simply clone it and change the hostname (from “my-app” to “my-app-stage”). Also change the environment
value into for example "stage"
.
{
nixosConfigurations."my-app" = nixpkgs.lib.nixosSystem {
inherit system;
specialArgs = attrs // {
environment = "production";
};
modules = [
({ config, pkgs, ... }: {
nixpkgs.overlays = [ overlay-unstable ];
})
./nixos/configuration.nix
];
};
+ nixosConfigurations."my-app-stage" = nixpkgs.lib.nixosSystem {
+ inherit system;
+ specialArgs = attrs // {
+ environment = "stage";
+ };
+ modules = [
+ ({ config, pkgs, ... }: {
+ nixpkgs.overlays = [ overlay-unstable ];
+ })
+ ./nixos/configuration.nix
+ ];
+ };
}
Create the staging server from Shipnix
When creating a new server, select the “Migrate” option.
After the server is provisioned, select the server preset that aligns with your running server and choose the Import a project already compatible with Shipnix
.
Follow the instructions to add your repo.
Then click deploy. If your project is properly configured to provision the server, you should be running a clone of your production server in terms of configuration.