2020-02-26 16:11:23 +00:00
|
|
|
#!/usr/bin/env bash
|
|
|
|
set -euo pipefail
|
|
|
|
|
|
|
|
# This script demonstrates how to setup a nix-bitcoin node in a NixOS container.
|
|
|
|
# Running this script leaves no traces on your host system.
|
|
|
|
|
|
|
|
# This demo is a template for your own experiments.
|
2021-02-01 21:53:06 +00:00
|
|
|
# Run with option `--interactive` or `-i` to start a shell for interacting with
|
|
|
|
# the node.
|
2020-02-26 16:11:23 +00:00
|
|
|
|
2023-01-03 18:36:27 +00:00
|
|
|
if [[ $EUID != 0 ]]; then
|
|
|
|
# NixOS containers require root permissions
|
|
|
|
exec sudo "${BASH_SOURCE[0]}" "$@"
|
2020-02-26 16:11:23 +00:00
|
|
|
fi
|
|
|
|
|
2023-01-03 18:36:27 +00:00
|
|
|
if [[ $(sysctl -n net.ipv4.ip_forward) != 1 ]]; then
|
2020-10-18 11:41:57 +00:00
|
|
|
echo "Error: IP forwarding (net.ipv4.ip_forward) is not enabled."
|
|
|
|
echo "Needed for container WAN access."
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
2023-01-03 18:36:27 +00:00
|
|
|
if [[ ! -v DEPLOY_CONTAINER_NIX_SHELL ]]; then
|
|
|
|
echo "Running script in nix shell env..."
|
|
|
|
cd "${BASH_SOURCE[0]%/*}"
|
|
|
|
DEPLOY_CONTAINER_NIX_SHELL=1 exec nix-shell --run "./${BASH_SOURCE[0]##*/} $*"
|
2020-10-18 11:41:56 +00:00
|
|
|
fi
|
|
|
|
|
2020-10-18 11:41:57 +00:00
|
|
|
interactive=
|
2021-09-23 14:51:34 +00:00
|
|
|
configuration=
|
2020-10-18 11:41:57 +00:00
|
|
|
for arg in "$@"; do
|
|
|
|
case $arg in
|
|
|
|
-i|--interactive)
|
|
|
|
interactive=1
|
|
|
|
;;
|
2021-09-23 14:51:34 +00:00
|
|
|
*)
|
|
|
|
configuration=$arg
|
2020-10-18 11:41:57 +00:00
|
|
|
;;
|
|
|
|
esac
|
|
|
|
done
|
|
|
|
|
2020-10-11 18:02:25 +00:00
|
|
|
# These commands can also be executed interactively in a shell session
|
|
|
|
demoCmds='
|
2020-02-26 16:11:23 +00:00
|
|
|
echo
|
|
|
|
echo "Bitcoind service:"
|
|
|
|
c systemctl status bitcoind
|
|
|
|
echo
|
|
|
|
echo "Bitcoind network:"
|
|
|
|
c bitcoin-cli getnetworkinfo
|
|
|
|
echo
|
|
|
|
echo "lightning-cli state:"
|
|
|
|
c lightning-cli getinfo
|
|
|
|
echo
|
|
|
|
echo "Bitcoind data dir:"
|
2022-06-27 22:08:29 +00:00
|
|
|
sudo ls -al /var/lib/*containers/demo-node/var/lib/bitcoind
|
2020-10-11 18:02:25 +00:00
|
|
|
'
|
2020-10-18 11:41:57 +00:00
|
|
|
nodeInfoCmd='
|
|
|
|
echo
|
|
|
|
echo "Node info:"
|
|
|
|
c nodeinfo
|
|
|
|
'
|
2020-10-11 18:02:25 +00:00
|
|
|
|
2021-09-23 14:51:34 +00:00
|
|
|
if [[ ! $configuration ]]; then
|
2020-10-18 11:41:57 +00:00
|
|
|
configuration=configuration.nix
|
|
|
|
demoCmds="${demoCmds}${nodeInfoCmd}"
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [[ $interactive ]]; then
|
2021-02-05 14:39:04 +00:00
|
|
|
runCmd=()
|
2020-10-18 11:41:57 +00:00
|
|
|
else
|
|
|
|
runCmd=(--run bash -c "$demoCmds")
|
|
|
|
fi
|
2020-02-26 16:11:23 +00:00
|
|
|
|
2020-10-11 18:02:25 +00:00
|
|
|
# Build container.
|
|
|
|
# Learn more: https://github.com/erikarvstedt/extra-container
|
|
|
|
#
|
2022-08-16 19:00:00 +00:00
|
|
|
read -rd '' src <<EOF || true
|
2020-10-11 18:02:25 +00:00
|
|
|
{ pkgs, lib, ... }: {
|
|
|
|
containers.demo-node = {
|
|
|
|
extra.addressPrefix = "10.250.0";
|
|
|
|
extra.enableWAN = true;
|
|
|
|
config = { pkgs, config, lib, ... }: {
|
|
|
|
imports = [
|
2021-09-23 14:51:34 +00:00
|
|
|
$(realpath "$configuration")
|
2020-10-11 18:02:25 +00:00
|
|
|
];
|
2021-03-10 13:08:34 +00:00
|
|
|
nix-bitcoin.generateSecrets = true;
|
2020-10-11 18:02:25 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
}
|
|
|
|
EOF
|
2020-10-18 11:41:56 +00:00
|
|
|
extra-container shell -E "$src" "${runCmd[@]}"
|
2020-04-15 16:54:02 +00:00
|
|
|
|
2020-10-11 18:02:25 +00:00
|
|
|
# The container is automatically deleted at exit
|