Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve examples #250

Merged
merged 4 commits into from
Oct 20, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 14 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ cd nix-bitcoin/examples/
nix-shell
```

The following example scripts set up a nix-bitcoin node according to `examples/configuration.nix` and then
The following example scripts set up a nix-bitcoin node according to [`examples/configuration.nix`](examples/configuration.nix) and then
shut down immediately. They leave no traces (outside of `/nix/store`) on the host system.

- [`./deploy-container.sh`](examples/deploy-container.sh) creates a [NixOS container](https://github.com/erikarvstedt/extra-container).\
Expand All @@ -48,8 +48,20 @@ shut down immediately. They leave no traces (outside of `/nix/store`) on the hos
NixOps can be used to deploy to various other backends like cloud providers.\
Requires: [Nix](https://nixos.org/nix/), [VirtualBox](https://www.virtualbox.org)

- [`./deploy-container-minimal.sh`](examples/deploy-container-minimal.sh) creates a
container defined by [minimal-configuration.nix](examples/minimal-configuration.nix) that
doesn't use the [secure-node.nix](modules/presets/secure-node.nix) preset.
Also shows how to use nix-bitcoin in an existing NixOS config.\
jonasnick marked this conversation as resolved.
Show resolved Hide resolved
Requires: [Nix](https://nixos.org/), a systemd-based Linux distro and root privileges

Run the examples with option `--interactive` or `-i` to start a shell for interacting with
the node:
```bash
./deploy-qemu-vm.sh -i
```

#### Tests
The internal test suite is also useful for exploring features.
The internal test suite is also useful for exploring features.\
The following `run-tests.sh` commands leave no traces (outside of `/nix/store`) on
the host system.

Expand Down
3 changes: 3 additions & 0 deletions examples/deploy-container-minimal.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/usr/bin/env bash

exec "${BASH_SOURCE[0]%/*}/deploy-container.sh" --minimal-config "$@"
53 changes: 38 additions & 15 deletions examples/deploy-container.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,35 @@ set -euo pipefail
# Feel free to modify or to run nix-shell and execute individual statements of this
# script in the interactive shell.

if [[ $(sysctl -n net.ipv4.ip_forward) != 1 ]]; then
if [[ ! -v IN_NIX_SHELL ]]; then
echo "Running script in nix shell env..."
cd "${BASH_SOURCE[0]%/*}"
exec nix-shell --run "./${BASH_SOURCE[0]##*/} $*"
fi

if [[ $(sysctl -n net.ipv4.ip_forward || sudo sysctl -n net.ipv4.ip_forward) != 1 ]]; then
echo "Error: IP forwarding (net.ipv4.ip_forward) is not enabled."
echo "Needed for container WAN access."
exit 1
fi

if [[ ! -v IN_NIX_SHELL ]]; then
echo "Running script in nix shell env..."
cd "${BASH_SOURCE[0]%/*}"
exec nix-shell --run "${BASH_SOURCE[0]}"
if [[ $EUID != 0 ]]; then
# NixOS containers require root permissions
exec sudo "PATH=$PATH" "NIX_PATH=$NIX_PATH" "IN_NIX_SHELL=$IN_NIX_SHELL" "${BASH_SOURCE[0]}" "$@"
fi

# Uncomment to start a container shell session
# interactive=1
interactive=
minimalConfig=
for arg in "$@"; do
case $arg in
-i|--interactive)
interactive=1
;;
--minimal-config)
minimalConfig=1
;;
esac
done

# These commands can also be executed interactively in a shell session
demoCmds='
Expand All @@ -35,14 +50,23 @@ echo
echo "lightning-cli state:"
c lightning-cli getinfo
echo
echo "Node info:"
c nodeinfo
echo
echo "Bitcoind data dir:"
sudo ls -al /var/lib/containers/demo-node/var/lib/bitcoind
'
nodeInfoCmd='
echo
echo "Node info:"
c nodeinfo
'

if [[ $minimalConfig ]]; then
configuration=minimal-configuration.nix
else
configuration=configuration.nix
demoCmds="${demoCmds}${nodeInfoCmd}"
fi

if [[ ${interactive:-} ]]; then
if [[ $interactive ]]; then
runCmd=
else
runCmd=(--run bash -c "$demoCmds")
Expand All @@ -51,21 +75,20 @@ fi
# Build container.
# Learn more: https://github.com/erikarvstedt/extra-container
#
read -d '' src <<'EOF' || true
read -d '' src <<EOF || true
{ pkgs, lib, ... }: {
containers.demo-node = {
extra.addressPrefix = "10.250.0";
extra.enableWAN = true;
config = { pkgs, config, lib, ... }: {
imports = [
<nix-bitcoin/examples/configuration.nix>
<nix-bitcoin/examples/${configuration}>
<nix-bitcoin/modules/secrets/generate-secrets.nix>
];
};
};
}
EOF
$([[ $EUID = 0 ]] || echo sudo "PATH=$PATH" "NIX_PATH=$NIX_PATH") \
$(type -P extra-container) shell -E "$src" "${runCmd[@]}"
extra-container shell -E "$src" "${runCmd[@]}"

# The container is automatically deleted at exit
10 changes: 7 additions & 3 deletions examples/deploy-nixops.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ set -euo pipefail
if [[ ! -v IN_NIX_SHELL ]]; then
echo "Running script in nix shell env..."
cd "${BASH_SOURCE[0]%/*}"
exec nix-shell --run "${BASH_SOURCE[0]}"
exec nix-shell --run "./${BASH_SOURCE[0]##*/} $*"
fi

# Cleanup on exit
Expand Down Expand Up @@ -40,7 +40,11 @@ nixops deploy -d bitcoin-node
nixops ssh bitcoin-node systemctl status bitcoind

c() { nixops ssh bitcoin-node "$@"; }
# Uncomment to start a shell session here
# . start-bash-session.sh

case ${1:-} in
-i|--interactive)
. start-bash-session.sh
;;
esac

# Cleanup happens at exit (see above)
9 changes: 6 additions & 3 deletions examples/deploy-qemu-vm.sh
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ set -euo pipefail
if [[ ! -v IN_NIX_SHELL ]]; then
echo "Running script in nix shell env..."
cd "${BASH_SOURCE[0]%/*}"
exec nix-shell --run "${BASH_SOURCE[0]}"
exec nix-shell --run "./${BASH_SOURCE[0]##*/} $*"
fi

tmpDir=/tmp/nix-bitcoin-qemu-vm
Expand Down Expand Up @@ -91,7 +91,10 @@ echo
echo "Node info:"
c nodeinfo

# Uncomment to start a shell session here
# . start-bash-session.sh
case ${1:-} in
-i|--interactive)
. start-bash-session.sh
;;
esac

# Cleanup happens at exit (see above)
22 changes: 22 additions & 0 deletions examples/minimal-configuration.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{ config, pkgs, lib, ... }: {
imports = [
<nix-bitcoin/modules/nix-bitcoin.nix>
<nix-bitcoin/modules/secrets/generate-secrets.nix>
];

services.bitcoind.enable = true;
services.clightning.enable = true;

# When using nix-bitcoin as part of a larger NixOS configuration, set the following to enable
# interactive access to nix-bitcoin features (like bitcoin-cli) for your system's main user
nix-bitcoin.operator = {
enable = true;
name = "main"; # Set this to your system's main user
};

# The system's main unprivileged user
users.users.main = {
isNormalUser = true;
password = "a";
};
}
6 changes: 5 additions & 1 deletion examples/start-bash-session.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@
USAGE_INFO='
Starting shell...
Run "c COMMAND" to execute a command on the bitcoin node
Run "c" to start a shell session inside the node'
Run "c" to start a shell session inside the node

Example:
c systemctl status bitcoind
'

# BASH_ENVIRONMENT contains definitions of read-only variables like 'BASHOPTS' that
# cause warnings on evaluation. Suppress these warnings while sourcing.
Expand Down