Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Tutorial: Install and configure NixOS for bitcoinix on a dedicated machine

This tutorial describes how to manage your Bitcoin node comfortably from your personal computer: you keep the node configuration in a Nix flake and deploy it over SSH with nixos-rebuild or Colmena. The deployment tool krops is described as a legacy alternative. However, bitcoinix is agnostic to the deployment method and can be used with different or without such tools (see examples).

0. Preparation

  1. Find a machine to deploy bitcoinix on (see hardware.md).

  2. Optional: Make sure you have the latest firmware for your system (BIOS, microcode updates).

  3. Optional: Disable Simultaneous Multi-Threading (SMT) in the BIOS

    Researchers recommend disabling (SMT), also known as Hyper-Threading Technology in the Intel® world to significantly reduce the impact of speculative execution-based attacks (https://mdsattacks.com/).

1. NixOS installation

  1. Install NixOS on the node as described in the NixOS manual. Use the minimal ISO image for the platform of your node (x86_64-linux or aarch64-linux) from https://nixos.org/download/. To encrypt the disk, see LUKS-Encrypted File Systems.

  2. Before you run nixos-install, enable SSH and add the SSH public key of the computer you deploy from to /mnt/etc/nixos/configuration.nix:

    services.openssh.enable = true;
    users.users.root.openssh.authorizedKeys.keys = [
      "ssh-ed25519 AAAA... you@your-computer" # FIXME
    ];
    

    The deployment logs in as root with this key. With the default services.openssh.settings.PermitRootLogin = "prohibit-password", root can’t log in over SSH with a password.

  3. Finish the installation as described in the manual, and reboot.

2. Nix installation

Run these steps on the computer you deploy from, not on the node.

  1. Install git, and install Nix in multi-user mode as described at https://nixos.org/download/. On NixOS, Nix is already installed.

  2. Enable flakes, which the deployment below uses

    mkdir -p ~/.config/nix
    echo "experimental-features = nix-command flakes" >> ~/.config/nix/nix.conf
    

    On NixOS, set nix.settings.experimental-features = [ "nix-command" "flakes" ]; instead.

  3. Optional: Disallow substitutes

    You can put substitute = false to your nix.conf usually found in /etc/nix/ to build the packages from source. This eliminates an attack vector where Nix’s build server or binary cache is compromised.

3. Get and verify bitcoinix

  1. Clone this project

    git clone https://github.com/bitcoinix/bitcoinix
    cd bitcoinix
    
  2. Verify the release signing keys

    A bitcoinix release is a signed git tag. The list of keys that may sign releases lives in the repository you just cloned, so it cannot vouch for itself. Print the fingerprints of the listed keys:

    grep -v '^#' helper/release-keys/ssh-allowed-signers | cut -d' ' -f3- | ssh-keygen -lf -
    

    The release tag key, SHA256:2o/0Op+64zKgF4oMaS0LGOiGPNDsibaqX6g8qMf5Hz0, is also published outside this repository at https://seberm.com/ssh.html. Verify that statement as described on that page and check that the same fingerprint is listed above. Do not continue if it is not.

  3. Verify and check out the latest release

    git config gpg.ssh.allowedSignersFile helper/release-keys/ssh-allowed-signers
    git config gpg.ssh.revocationFile helper/release-keys/ssh-revoked-keys
    git tag -l 'v*' --sort=-v:refname | head -1    # the latest release, e.g. v0.1.0
    git tag -v v<version>
    git checkout v<version>
    cd ..
    

    git tag -v must report Good "git" signature with the release tag key fingerprint from the previous step. Checking out the tag makes sure that the files used below, including fetch-release.sh and its key files, are the ones that were signed.

4. Deploy with a flake

Your node configuration is a Nix flake that has bitcoinix as an input. You deploy it from your computer over SSH with nixos-rebuild or Colmena. CI builds both examples on code-changing pull requests and pushes to master; documentation-only PRs skip these builds.

  1. Copy an example to a new directory next to the bitcoinix clone

    For nixos-rebuild, use examples/flakes. For Colmena, use examples/colmena, a larger node. Both use the secure-node preset.

    cp -r bitcoinix/examples/flakes node-config    # or bitcoinix/examples/colmena
    cd node-config
    git init
    

    Flakes only see files that git tracks, so git add every file you create.

  2. Pin the release that you verified in step 3

    In flake.nix, set the bitcoinix input to the release tag:

    bitcoinix.url = "github:bitcoinix/bitcoinix/v<version>";
    

    Then lock the inputs and check that the locked commit is the one you verified:

    nix flake lock
    nix flake metadata --json | nix run nixpkgs#jq -- -r .locks.nodes.bitcoinix.locked.rev
    git -C ../bitcoinix rev-parse 'v<version>^{commit}'
    

    Both commands must print the same commit. Commit flake.lock, and repeat this step for every update.

  3. Adapt the configuration

    Copy hardware-configuration.nix from your node and import it in place of the example’s placeholders (in the Colmena example, replace configs/fullnode/hardware-configuration.nix):

    scp root@<node>:/etc/nixos/hardware-configuration.nix .
    

    Edit all lines marked FIXME. Above all, add your SSH public key to users.users.root.openssh.authorizedKeys.keys: the deployment logs in as root, and without the key you lose remote access to the node.

  4. Deploy

    With nixos-rebuild:

    nix run nixpkgs#nixos-rebuild -- switch --flake .#fullnode --target-host root@<node>
    

    This builds the system on your computer and copies it to the node. To build on the node instead, for example when you deploy from macOS, add --build-host root@<node>.

    With Colmena, set deployment.targetHost in flake.nix, then:

    nix develop
    colmena apply --on fullnode
    

    switch (the Colmena default) activates the new system on the running node, which can cause problems. For updates, above all of bitcoinix, prefer boot and a reboot: replace switch with boot in the nixos-rebuild command, or run colmena apply --on fullnode boot, then ssh root@<node> reboot.

  5. With the secure-node preset, you can now log in as the unprivileged operator user

    ssh operator@<node>
    

    bitcoinix authorizes the SSH keys of root for operator. Use operator for normal system management, and log in as root as rarely as possible.

5. Alternative: deploy with krops (legacy)

Note

krops is the deployment tool of nix-bitcoin, the predecessor project. Support for it may be removed in a future release. Prefer a flake-based deployment.

Set up the deployment directory

These steps continue from step 3 above.

  1. Create a new directory for your bitcoinix node config and copy initial files from bitcoinix

    mkdir nix-bitcoin-node
    cd nix-bitcoin-node
    cp -r ../bitcoinix/examples/{configuration.nix,shell.nix,krops,.gitignore} .
    
  2. Obtain the hash of the latest bitcoinix release

    ../bitcoinix/helper/fetch-release.sh > nix-bitcoin-release.nix
    

    fetch-release picks the highest signed release tag, verifies it against helper/release-keys/ssh-allowed-signers and helper/release-keys/ssh-revoked-keys, and refuses to emit a hash for an unsigned tag, or for a tag signed by an unknown or revoked key. Pass releaseVersion=<version> to pin a specific release. Set checkDownload=1 to additionally download the release archive into a throwaway Nix store and check that it is exactly the signed tree.

Optional: Specify the system of your node

This enables evaluating your node config on a machine that has a different system platform than your node.
Examples: Deploying from macOS or deploying from an x86 desktop PC to an ARM node.

# Run this when your node has a 64-bit x86 CPU (e.g., an Intel or AMD CPU)
echo "x86_64-linux" > krops/system

# Run this when your node has a 64-bit ARM CPU
echo "aarch64-linux" > krops/system

Deploy with krops

  1. Edit your SSH config

    nano ~/.ssh/config
    

    and add the node with an entry similar to the following (make sure to fix Hostname and IdentityFile):

    Host bitcoin-node
        # FIXME
        Hostname NODE_IP_ADDRESS_OR_HOST_NAME_HERE
        User root
        PubkeyAuthentication yes
        # FIXME
        IdentityFile ~/.ssh/id_...
        AddKeysToAgent yes
    
  2. Make sure you are in the deployment directory and edit krops/deploy.nix

    nano krops/deploy.nix
    

    Locate the FIXME and set the target to the name of the SSH config entry created earlier, i.e. bitcoin-node.

    Note that any file imported by your configuration.nix must be copied to the target machine by krops. For example, if there is an import of networking.nix you must add it to extraSources in krops/deploy.nix like this:

    extraSources = {
        "hardware-configuration.nix".file = toString ../hardware-configuration.nix;
        "networking.nix".file = toString ../networking.nix;
    };
    
  3. Optional: Disallow substitutes

    If you prefer to build the system from source instead of copying binaries from the Nix cache, add the following line to configuration.nix:

    nix.settings.substitute = false;
    

    If the build process fails for some reason when deploying with krops-deploy (see later step), it may be difficult to find the cause due to the missing output. To see the build output, SSH into the target machine and run

    nixos-rebuild -I /var/src switch
    
  4. Copy hardware-configuration.nix from your node to the deployment directory.

    scp root@bitcoin-node:/etc/nixos/hardware-configuration.nix .
    
  5. Adjust configuration by opening the configuration.nix file and enable/disable the modules you want by editing this file.

    nano configuration.nix
    

    Pay attention to lines that are preceded by FIXME comments. In particular:

    1. Make sure to set your SSH pubkey. Otherwise, you lose remote access: root can only log in over SSH with a key.
    2. Uncomment the line ./hardware-configuration.nix by removing #.
  6. Enter the deployment environment

    nix-shell
    
  7. Deploy with krops in nix-shell

    deploy
    

    This will now create a bitcoinix node on the target machine.

  8. You can now access bitcoin-node via SSH

    ssh operator@bitcoin-node
    

    Note that you’re able to log in as the unprivileged operator user because bitcoinix automatically authorizes the SSH key added to root.

For security reasons, all normal system management tasks can and should be performed with the operator user. Logging in as root should be done as rarely as possible.

See also: