Skip to content

Instantly share code, notes, and snippets.

@chris-martin
Created March 7, 2018 17:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chris-martin/657f855bf844a35d8533349f372bd57f to your computer and use it in GitHub Desktop.
Save chris-martin/657f855bf844a35d8533349f372bd57f to your computer and use it in GitHub Desktop.
#! /usr/bin/env bash
set -e # Exit immediately if a command exits with a non-zero status.
set -u # Treat unset variables as an error when substituting
# The .nix directory is where we put all our Nix output symlinks. Create it if it doesn't already exist.
mkdir --parents .nix
# Pin Nix to the exact version of nixpkgs we're using.
nix build -f nixpkgs.nix --out-link .nix/nixpkgs
export NIX_PATH=nixpkgs=$PWD/.nix/nixpkgs
# Build NixOS for the server.
nix build -v '(import ./default.nix).webserver1' \
--out-link .nix/webserver1
# Get the path of the NixOS build in /nix/store.
NIXOS=$(readlink .nix/webserver1)
# Upload the build to the server.
nix copy "$NIXOS" --to ssh://example.com --no-check-sigs
# Set the system profile to the new build.
ssh example.com \
sudo nix-env --profile /nix/var/nix/profiles/system --set "$NIXOS"
# Activate the new profile
ssh example.com \
sudo /nix/var/nix/profiles/system/bin/switch-to-configuration switch
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment