From 82fa89b57e96d1987ee5400dab4e0461a59b41ba Mon Sep 17 00:00:00 2001 From: Qubasa Date: Fri, 10 May 2024 15:39:46 +0200 Subject: [PATCH] Fix template. Improve docu. Add disko as default imported module. --- clanModules/flake-module.nix | 7 ++--- docs/mkdocs.yml | 2 ++ docs/site/getting-started/configure.md | 10 +++---- docs/site/getting-started/machines.md | 2 +- nixosModules/flake-module.nix | 2 +- pkgs/clan-cli/clan_cli/secrets/key.py | 37 ++++++++++++++++++++------ pkgs/installer/flake-module.nix | 3 ++- templates/new-clan/flake.nix | 8 +++--- templates/new-clan/modules/shared.nix | 1 - 9 files changed, 46 insertions(+), 26 deletions(-) diff --git a/clanModules/flake-module.nix b/clanModules/flake-module.nix index 1a80b37df..28ed84750 100644 --- a/clanModules/flake-module.nix +++ b/clanModules/flake-module.nix @@ -1,11 +1,8 @@ -{ inputs, ... }: +{ ... }: { flake.clanModules = { disk-layouts = { - imports = [ - ./disk-layouts - inputs.disko.nixosModules.default - ]; + imports = [ ./disk-layouts ]; }; borgbackup = ./borgbackup; ergochat = ./ergochat; diff --git a/docs/mkdocs.yml b/docs/mkdocs.yml index e19a91021..a2ce50a86 100644 --- a/docs/mkdocs.yml +++ b/docs/mkdocs.yml @@ -27,6 +27,8 @@ markdown_extensions: - pymdownx.details - pymdownx.highlight: use_pygments: true + anchor_linenums: true + linenums: true - toc: title: On this page diff --git a/docs/site/getting-started/configure.md b/docs/site/getting-started/configure.md index 6ccf6d744..d63804cf4 100644 --- a/docs/site/getting-started/configure.md +++ b/docs/site/getting-started/configure.md @@ -63,12 +63,12 @@ Adding or configuring a new machine requires two simple steps: 1. Find the remote disk id by executing: ```bash title="setup computer" - ssh root@ lsblk --output NAME,ID-LINK,FSTYPE,SIZE,MOUNTPOINT + ssh root@flash-installer.local lsblk --output NAME,ID-LINK,FSTYPE,SIZE,MOUNTPOINT ``` Which should show something like: - ```bash + ```bash hl_lines="6" NAME ID-LINK FSTYPE SIZE MOUNTPOINT sda usb-ST_16GB_AA6271026J1000000509-0:0 14.9G ├─sda1 usb-ST_16GB_AA6271026J1000000509-0:0-part1 1M @@ -84,7 +84,7 @@ Adding or configuring a new machine requires two simple steps: === "**buildClan**" - ```nix title="clan-core.lib.buildClan" + ```nix title="clan-core.lib.buildClan" hl_lines="17 13" buildClan { # ... machines = { @@ -114,7 +114,7 @@ Adding or configuring a new machine requires two simple steps: - ```nix title="clan-core.flakeModules.default" + ```nix title="clan-core.flakeModules.default" hl_lines="17,13" clan = { # ... machines = { @@ -148,7 +148,7 @@ Adding or configuring a new machine requires two simple steps: 1. Generate a `hardware-configuration.nix` for your target computer ```bash - ssh root@ nixos-generate-config --no-filesystems --show-hardware-config > machines/jon/hardware-configuration.nix + ssh root@flash-installer.local nixos-generate-config --no-filesystems --show-hardware-config > machines/jon/hardware-configuration.nix ``` ### Initialize the facts diff --git a/docs/site/getting-started/machines.md b/docs/site/getting-started/machines.md index ab25d2e7e..361aabb92 100644 --- a/docs/site/getting-started/machines.md +++ b/docs/site/getting-started/machines.md @@ -25,7 +25,7 @@ This process involves preparing a suitable hardware and disk partitioning config 2. Boot the target machine and connect it to a network that makes it reachable from your setup computer. -=== "**Baremetal Machines**" +=== "**Remote Machines**" - [x] **Two Computers**: You need one computer that you're getting ready (we'll call this the Target Computer) and another one to set it up from (we'll call this the Setup Computer). Make sure both can talk to each other over the network using SSH. - [x] **Machine configuration**: See our basic [configuration guide](./configure.md) diff --git a/nixosModules/flake-module.nix b/nixosModules/flake-module.nix index e60126947..4f34d4933 100644 --- a/nixosModules/flake-module.nix +++ b/nixosModules/flake-module.nix @@ -5,10 +5,10 @@ installer.imports = [ ./installer self.nixosModules.hidden-ssh-announce - inputs.disko.nixosModules.disko ]; clanCore.imports = [ inputs.sops-nix.nixosModules.sops + inputs.disko.nixosModules.default ./clanCore ./iso ( diff --git a/pkgs/clan-cli/clan_cli/secrets/key.py b/pkgs/clan-cli/clan_cli/secrets/key.py index 8d13714e7..844e6960a 100644 --- a/pkgs/clan-cli/clan_cli/secrets/key.py +++ b/pkgs/clan-cli/clan_cli/secrets/key.py @@ -1,19 +1,44 @@ import argparse +import logging from pathlib import Path from clan_cli.git import commit_files -from .. import tty from ..errors import ClanError from .secrets import update_secrets from .sops import default_sops_key_path, generate_private_key, get_public_key +log = logging.getLogger(__name__) + + +def extract_public_key(filepath: Path) -> str: + """ + Extracts the public key from a given text file. + """ + try: + with open(filepath) as file: + for line in file: + # Check if the line contains the public key + if line.startswith("# public key:"): + # Extract and return the public key part after the prefix + return line.strip().split(": ")[1] + except FileNotFoundError: + raise ClanError(f"The file at {filepath} was not found.") + except Exception as e: + raise ClanError(f"An error occurred while extracting the public key: {e}") + + raise ClanError(f"Could not find the public key in the file at {filepath}.") + def generate_key() -> str: path = default_sops_key_path() if path.exists(): - raise ClanError(f"Key already exists at {path}") + log.info(f"Key already exists at {path}") + return extract_public_key(path) priv_key, pub_key = generate_private_key(out_file=path) + log.info( + f"Generated age private key at '{default_sops_key_path()}' for your user. Please back it up on a secure location or you will lose access to your secrets." + ) return pub_key @@ -23,13 +48,9 @@ def show_key() -> str: def generate_command(args: argparse.Namespace) -> None: pub_key = generate_key() - tty.info( - f"Generated age private key at '{default_sops_key_path()}' for your user. Please back it up on a secure location or you will lose access to your secrets." + log.info( + f"Also add your age public key to the repository with: \nclan secrets users add {pub_key}" ) - tty.info( - f"Also add your age public key to the repository with 'clan secrets users add youruser {pub_key}' (replace youruser with your user name)" - ) - pass def show_command(args: argparse.Namespace) -> None: diff --git a/pkgs/installer/flake-module.nix b/pkgs/installer/flake-module.nix index a3babf5a6..69f32e908 100644 --- a/pkgs/installer/flake-module.nix +++ b/pkgs/installer/flake-module.nix @@ -1,7 +1,7 @@ { self, lib, ... }: let installerModule = - { config, pkgs, ... }: + { config, ... }: { imports = [ self.nixosModules.installer @@ -29,6 +29,7 @@ let installer = lib.nixosSystem { modules = [ + self.inputs.disko.nixosModules.default installerModule { disko.memSize = 4096; } # FIXME: otherwise the image builder goes OOM ]; diff --git a/templates/new-clan/flake.nix b/templates/new-clan/flake.nix index d3f9fd1ed..d61938ee0 100644 --- a/templates/new-clan/flake.nix +++ b/templates/new-clan/flake.nix @@ -35,8 +35,8 @@ # TODO: Example how to use disko for more complicated setups - # remote> lsblk --output NAME,PTUUID,FSTYPE,SIZE,MOUNTPOINT - clan.disk-layouts.singleDiskExt4 = { + # ssh root@flash-installer.local lsblk --output NAME,ID-LINK,FSTYPE,SIZE,MOUNTPOINT + disko.devices.disk.main = { device = "/dev/disk/by-id/__CHANGE_ME__"; }; @@ -59,8 +59,8 @@ # local> clan facts generate - # remote> lsblk --output NAME,PTUUID,FSTYPE,SIZE,MOUNTPOINT - clan.disk-layouts.singleDiskExt4 = { + # ssh root@flash-installer.local lsblk --output NAME,ID-LINK,FSTYPE,SIZE,MOUNTPOINT + disko.devices.disk.main = { device = "/dev/disk/by-id/__CHANGE_ME__"; }; /* diff --git a/templates/new-clan/modules/shared.nix b/templates/new-clan/modules/shared.nix index 56477b433..bcd3118ec 100644 --- a/templates/new-clan/modules/shared.nix +++ b/templates/new-clan/modules/shared.nix @@ -2,7 +2,6 @@ { imports = [ clan-core.clanModules.sshd - clan-core.clanModules.disk-layouts clan-core.clanModules.root-password ]; }