From e2d1a946b11b85a1fdf5d657cb5cb2d3e47bfcba Mon Sep 17 00:00:00 2001 From: Ilan Joselevich Date: Thu, 12 Jan 2023 04:34:12 +0200 Subject: [PATCH] treewide: add formatting via treefmt --- bors.toml | 2 + flake.lock | 18 +++- flake.nix | 14 +++ src/nixos-remote.sh | 135 ++++++++++++++-------------- tests/lib/test-base.nix | 4 +- tests/modules/system-to-install.nix | 6 +- 6 files changed, 105 insertions(+), 74 deletions(-) diff --git a/bors.toml b/bors.toml index 3581f1b..d69cae5 100644 --- a/bors.toml +++ b/bors.toml @@ -7,6 +7,8 @@ status = [ "package default [x86_64-linux]", "check from-nixos-with-sudo [x86_64-linux]", "package nixos-remote [x86_64-linux]", + "check treefmt [x86_64-linux]", + "package treefmt [x86_64-linux]", # github actions "deploy" ] diff --git a/flake.lock b/flake.lock index ee4cc52..1572c78 100644 --- a/flake.lock +++ b/flake.lock @@ -113,7 +113,23 @@ "disko": "disko", "flake-parts": "flake-parts", "nixos-images": "nixos-images", - "nixpkgs": "nixpkgs" + "nixpkgs": "nixpkgs", + "treefmt-nix": "treefmt-nix" + } + }, + "treefmt-nix": { + "locked": { + "lastModified": 1672931382, + "narHash": "sha256-lgtc2Sct/xtvqkdzlJ4AL3Vesw0Wz/fxqNGOBFS7YXU=", + "owner": "numtide", + "repo": "treefmt-nix", + "rev": "6717065d6a432bc3f5b827007ad959e6032d5856", + "type": "github" + }, + "original": { + "owner": "numtide", + "repo": "treefmt-nix", + "type": "github" } } }, diff --git a/flake.nix b/flake.nix index f9d4fe2..5d3aec2 100644 --- a/flake.nix +++ b/flake.nix @@ -7,6 +7,8 @@ disko = { url = "github:nix-community/disko/master"; inputs.nixpkgs.follows = "nixpkgs"; }; # used for testing nixos-images.url = "github:nix-community/nixos-images"; + # used for development + treefmt-nix.url = "github:numtide/treefmt-nix"; }; @@ -17,6 +19,18 @@ ./src/flake-module.nix ./tests/flake-module.nix ./docs/flake-module.nix + inputs.treefmt-nix.flakeModule ]; + + perSystem = { config, ... }: { + treefmt = { + projectRootFile = "flake.nix"; + programs.nixpkgs-fmt.enable = true; + programs.shellcheck.enable = true; + programs.shfmt.enable = true; + settings.formatter.shellcheck.options = [ "-s" "bash" ]; + }; + formatter = config.treefmt.build.wrapper; + }; }; } diff --git a/src/nixos-remote.sh b/src/nixos-remote.sh index 4c3ddca..7a3e17d 100755 --- a/src/nixos-remote.sh +++ b/src/nixos-remote.sh @@ -42,63 +42,62 @@ declare -A disk_encryption_keys while [[ $# -gt 0 ]]; do case "$1" in - -f | --flake) - flake=$2 - shift - ;; - -L | --print-build-logs) - print_build_logs=y - ;; - -s | --store-paths) - disko_script=$(readlink -f "$2") - nixos_system=$(readlink -f "$3") - shift - shift - ;; - --help) + -f | --flake) + flake=$2 + shift + ;; + -L | --print-build-logs) + print_build_logs=y + ;; + -s | --store-paths) + disko_script=$(readlink -f "$2") + nixos_system=$(readlink -f "$3") + shift + shift + ;; + --help) + showUsage + exit 0 + ;; + --kexec) + kexec_url=$2 + shift + ;; + --no-ssh-copy-id) + no_ssh_copy=y + ;; + --debug) + enable_debug="-x" + print_build_logs=y + set -x + ;; + --extra-files) + extra_files=$2 + shift + ;; + --disk-encryption-keys) + disk_encryption_keys["$2"]="$3" + shift + shift + ;; + --stop-after-disko) + stop_after_disko=y + ;; + --no-reboot) + maybereboot="" + ;; + *) + if [[ -z ${ssh_connection:-} ]]; then + ssh_connection="$1" + else showUsage - exit 0 - ;; - --kexec) - kexec_url=$2 - shift - ;; - --no-ssh-copy-id) - no_ssh_copy=y - ;; - --debug) - enable_debug="-x" - print_build_logs=y - set -x - ;; - --extra-files) - extra_files=$2 - shift - ;; - --disk-encryption-keys) - disk_encryption_keys["$2"]="$3" - shift - shift - ;; - --stop-after-disko) - stop_after_disko=y - ;; - --no-reboot) - maybereboot="" - ;; - *) - if [[ -z ${ssh_connection:-} ]]; then - ssh_connection="$1" - else - showUsage - exit 1 - fi - ;; + exit 1 + fi + ;; esac shift done - # ssh wrapper timeout_ssh_() { timeout 10 ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no "$ssh_connection" "$@" @@ -134,20 +133,20 @@ if [[ -z ${ssh_connection:-} ]]; then fi # parse flake nixos-install style syntax, get the system attr -if [[ -n "${flake:-}" ]]; then +if [[ -n ${flake:-} ]]; then if [[ $flake =~ ^(.*)\#([^\#\"]*)$ ]]; then - flake="${BASH_REMATCH[1]}" - flakeAttr="${BASH_REMATCH[2]}" + flake="${BASH_REMATCH[1]}" + flakeAttr="${BASH_REMATCH[2]}" fi - if [[ -z "${flakeAttr:-}" ]]; then + if [[ -z ${flakeAttr:-} ]]; then echo "Please specify the name of the NixOS configuration to be installed, as a URI fragment in the flake-uri." - echo "For example, to use the output nixosConfigurations.foo from the flake.nix, append \"#foo\" to the flake-uri." + echo 'For example, to use the output nixosConfigurations.foo from the flake.nix, append "#foo" to the flake-uri.' exit 1 fi disko_script=$(nix_build "${flake}#nixosConfigurations.${flakeAttr}.config.system.build.disko") nixos_system=$(nix_build "${flake}#nixosConfigurations.${flakeAttr}.config.system.build.toplevel") -elif [[ -n "${disko_script:-}" ]] && [[ -n "${nixos_system:-}" ]]; then - if [[ ! -e "${disko_script}" ]] || [[ ! -e "${nixos_system}" ]]; then +elif [[ -n ${disko_script:-} ]] && [[ -n ${nixos_system:-} ]]; then + if [[ ! -e ${disko_script} ]] || [[ ! -e ${nixos_system} ]]; then echo "${disko_script} and ${nixos_system} must be existing store-paths" exit 1 fi @@ -160,7 +159,8 @@ fi # TODO we probably need an architecture detection here # TODO if we have specified a user here but we are already booted into the # installer, than the user might not work anymore -until facts=$(ssh_ -o ConnectTimeout=10 -- </dev/null && echo "y" || echo "n" @@ -198,14 +198,14 @@ if [[ ${is_kexec-n} != "y" ]] && [[ ${no_ssh_copy-n} != "y" ]]; then fi if [[ ${is_kexec-n} == "n" ]]; then - ssh_ << SSH + ssh_ < $path" < "${disk_encryption_keys[$path]}" + ssh_ "umask 077; cat > $path" <"${disk_encryption_keys[$path]}" done nix_copy --to "ssh://$ssh_connection" "$disko_script" @@ -242,7 +241,7 @@ fi nix_copy --to "ssh://$ssh_connection?remote-store=local?root=/mnt" "$nixos_system" if [[ -n ${extra_files:-} ]]; then - if [[ -d "$extra_files" ]]; then + if [[ -d $extra_files ]]; then extra_files="$extra_files/" fi rsync -vrlF -e "ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no" "$extra_files" "${ssh_connection}:/mnt/" diff --git a/tests/lib/test-base.nix b/tests/lib/test-base.nix index eff30b4..0ec42c6 100644 --- a/tests/lib/test-base.nix +++ b/tests/lib/test-base.nix @@ -1,8 +1,8 @@ test: -{ pkgs ? import {}, ... } @ args: +{ pkgs ? import { }, ... } @ args: let inherit (pkgs) lib; - nixos-lib = import (pkgs.path + "/nixos/lib") {}; + nixos-lib = import (pkgs.path + "/nixos/lib") { }; in (nixos-lib.runTest { hostPkgs = pkgs; diff --git a/tests/modules/system-to-install.nix b/tests/modules/system-to-install.nix index e8ecf40..2236234 100644 --- a/tests/modules/system-to-install.nix +++ b/tests/modules/system-to-install.nix @@ -1,4 +1,4 @@ -{modulesPath, self, ...}: { +{ modulesPath, self, ... }: { imports = [ (modulesPath + "/testing/test-instrumentation.nix") (modulesPath + "/profiles/qemu-guest.nix") @@ -9,7 +9,7 @@ hardware.enableAllFirmware = false; networking.hostId = "8425e349"; # from profiles/base.nix, needed for zfs boot.zfs.devNodes = "/dev/disk/by-uuid"; # needed because /dev/disk/by-id is empty in qemu-vms - boot.loader.grub.devices = ["/dev/vda"]; + boot.loader.grub.devices = [ "/dev/vda" ]; disko.devices = { disk = { vda = { @@ -25,7 +25,7 @@ start = "0"; end = "1M"; part-type = "primary"; - flags = ["bios_grub"]; + flags = [ "bios_grub" ]; } { type = "partition";