nix fmt and fixes

This commit is contained in:
Shelvacu
2025-06-05 17:21:02 -07:00
committed by Shelvacu on fw
parent c5eebadff9
commit 275a3a447e
6 changed files with 90 additions and 80 deletions

View File

@@ -1,7 +1,4 @@
{ { lib, ... }:
lib,
...
}:
let let
inherit (builtins) stringLength substring; inherit (builtins) stringLength substring;
# aka startsWith but hopefully clear from the name what order the arguments go # aka startsWith but hopefully clear from the name what order the arguments go
@@ -30,23 +27,23 @@ let
}: }:
lines: lines:
let let
pipeline = [ pipeline =
(lib.splitString "\n") [
(list: (lib.splitString "\n")
if (builtins.length list) == 0 then list (
else if (lib.last list) == "" then lib.dropEnd 1 list list:
else list if (builtins.length list) == 0 then
) list
] else if (lib.last list) == "" then
++ lib.optional inlineComments (map (s: lib.dropEnd 1 list
builtins.head (lib.splitString "#" s) else
)) list
++ lib.optional trim (map lib.trim) )
++ lib.optional comments (builtins.filter (s: ]
(builtins.substring 0 1 s) != "#" ++ lib.optional inlineComments (map (s: builtins.head (lib.splitString "#" s)))
)) ++ lib.optional trim (map lib.trim)
++ lib.optional removeEmpty (builtins.filter (s: s != "")) ++ lib.optional comments (builtins.filter (s: (builtins.substring 0 1 s) != "#"))
; ++ lib.optional removeEmpty (builtins.filter (s: s != ""));
in in
lib.pipe lines pipeline; lib.pipe lines pipeline;
in in

View File

@@ -75,7 +75,7 @@ let
stringToPackageSet = stringToPackageSet =
from: from:
lib.pipe from [ lib.pipe from [
(vaculib.listOfLines {}) (vaculib.listOfLines { })
(map nameToPackageSet) (map nameToPackageSet)
builtins.listToAttrs builtins.listToAttrs
]; ];

View File

@@ -1,3 +1,5 @@
# the entire point is to be sh-compatible
# shellcheck disable=SC2292
if [ -z "${BASH_VERSINFO-}" ]; then if [ -z "${BASH_VERSINFO-}" ]; then
echo "shellvaculib.bash: This script only works with bash" >&2 echo "shellvaculib.bash: This script only works with bash" >&2
exit 1 exit 1
@@ -10,7 +12,7 @@ if ! (return 0 2>/dev/null); then
exit 1 exit 1
fi fi
if [[ "${SHELLVACULIB_COMPAT-0}" == 1 ]]; then if [[ ${SHELLVACULIB_COMPAT-0} == 1 ]]; then
: # nothing : # nothing
else else
set -euo pipefail set -euo pipefail
@@ -24,15 +26,17 @@ svl_eprintln() {
} }
_shellvaculib_debug_enabled() { _shellvaculib_debug_enabled() {
[[ "${SHELLVACULIB_DEBUG-0}" == 1 ]] [[ ${SHELLVACULIB_DEBUG-0} == 1 ]]
} }
_shellvaculib_debug_print() { _shellvaculib_debug_print() {
# shellcheck disable=SC2310
if _shellvaculib_debug_enabled; then if _shellvaculib_debug_enabled; then
svl_eprintln "$@" svl_eprintln "$@"
fi fi
} }
# shellcheck disable=SC2310
if _shellvaculib_debug_enabled; then if _shellvaculib_debug_enabled; then
svl_eprintln "shellvaculib.bash sourced." svl_eprintln "shellvaculib.bash sourced."
declare dollar_zero dollar_underscore declare dollar_zero dollar_underscore
@@ -125,7 +129,7 @@ if _shellvaculib_debug_enabled; then
"${cmd[@]}" || true "${cmd[@]}" || true
fi fi
if [[ -z "${_shellvaculib_arg0_canonicalized-}" ]]; then if [[ -z ${_shellvaculib_arg0_canonicalized-} ]]; then
if ! _shellvaculib_arg0_canonicalized="$(realpath -- "$0")"; then if ! _shellvaculib_arg0_canonicalized="$(realpath -- "$0")"; then
svl_eprintln "warn: could not get realpath of \$0: $0" svl_eprintln "warn: could not get realpath of \$0: $0"
fi fi
@@ -154,9 +158,9 @@ svl_throw_skip() {
shift shift
declare -a args=("$@") declare -a args=("$@")
#always skip svl_throw_skip itself #always skip svl_throw_skip itself
skip=$(( skip + 1 )) skip=$((skip + 1))
for (( i=${#FUNCNAME[@]}-1; i >= skip; i-- )); do for ((i = ${#FUNCNAME[@]} - 1; i >= skip; i--)); do
svl_err "in ${FUNCNAME[i]}[${BASH_LINENO[i-1]}]:" svl_err "in ${FUNCNAME[i]}[${BASH_LINENO[i - 1]}]:"
done done
svl_die "${args[@]}" svl_die "${args[@]}"
} }
@@ -169,26 +173,26 @@ declare -gi _shellvaculib_max_args=1000000
_shellvaculib_min_andor_max_args_impl() { _shellvaculib_min_andor_max_args_impl() {
declare -i actual_count="$1" minimum_count="$2" maximum_count="$3" declare -i actual_count="$1" minimum_count="$2" maximum_count="$3"
if (( minimum_count <= actual_count )) && (( actual_count <= maximum_count )); then if ((minimum_count <= actual_count)) && ((actual_count <= maximum_count)); then
return 0 return 0
fi fi
if (( actual_count < 0 )) || (( minimum_count < 0 )) || (( maximum_count < 0 )); then if ((actual_count < 0)) || ((minimum_count < 0)) || ((maximum_count < 0)); then
svl_die "this shouldn't happen (one of the counts negative in ${FUNCNAME[0]})" svl_die "this shouldn't happen (one of the counts negative in ${FUNCNAME[0]})"
fi fi
local expect_message local expect_message
if (( minimum_count == maximum_count )); then if ((minimum_count == maximum_count)); then
expect_message="expected exactly $minimum_count argument(s)" expect_message="expected exactly $minimum_count argument(s)"
elif (( minimum_count == 0 )) && (( maximum_count != _shellvaculib_max_args )); then elif ((minimum_count == 0)) && ((maximum_count != _shellvaculib_max_args)); then
expect_message="expected at most $maximum_count argument(s)" expect_message="expected at most $maximum_count argument(s)"
elif (( minimum_count != 0 )) && (( maximum_count == _shellvaculib_max_args )); then elif ((minimum_count != 0)) && ((maximum_count == _shellvaculib_max_args)); then
expect_message="expected at least $minimum_count argument(s)" expect_message="expected at least $minimum_count argument(s)"
elif (( minimum_count != 0 )) && (( maximum_count != _shellvaculib_max_args )); then elif ((minimum_count != 0)) && ((maximum_count != _shellvaculib_max_args)); then
expect_message="expected between $minimum_count and $maximum_count arguments" expect_message="expected between $minimum_count and $maximum_count arguments"
else else
svl_die "this shouldnt be possible really" svl_die "this shouldnt be possible really"
fi fi
local error_message="Wrong number of arguments, $expect_message, got $actual_count" local error_message="Wrong number of arguments, $expect_message, got $actual_count"
if [[ "${#FUNCNAME[@]}" == 1 ]]; then if [[ ${#FUNCNAME[@]} == 1 ]]; then
#we are being called from the top-level #we are being called from the top-level
svl_die "$error_message" svl_die "$error_message"
else else
@@ -229,7 +233,7 @@ svl_idempotent_add_prompt_command() {
PROMPT_COMMAND[0]=''${PROMPT_COMMAND[0]:-} PROMPT_COMMAND[0]=''${PROMPT_COMMAND[0]:-}
declare to_add="$1" cmd declare to_add="$1" cmd
for cmd in "${PROMPT_COMMAND[@]}"; do for cmd in "${PROMPT_COMMAND[@]}"; do
if [[ "$to_add" == "$cmd" ]]; then if [[ $to_add == "$cmd" ]]; then
return 0 return 0
fi fi
done done
@@ -240,22 +244,23 @@ svl_idempotent_add_prompt_command() {
# because the folder containing the script as well as PWD can be deleted while we're using it (or its parents), it's impossible to know for sure. Woohoo! # because the folder containing the script as well as PWD can be deleted while we're using it (or its parents), it's impossible to know for sure. Woohoo!
svl_probably_in_script_dir() { svl_probably_in_script_dir() {
declare script_dir canon_pwd declare script_dir canon_pwd
if [[ -z "$_shellvaculib_arg0_canonicalized" ]]; then if [[ -z $_shellvaculib_arg0_canonicalized ]]; then
_shellvaculib_debug_print "svl_probably_in_script_dir called when _shellvaculib_arg0_canonicalized is unset or blank, always returning false" _shellvaculib_debug_print "svl_probably_in_script_dir called when _shellvaculib_arg0_canonicalized is unset or blank, always returning false"
return 1 return 1
fi fi
if ! script_dir="$(dirname -- "$_shellvaculib_arg0_canonicalized")"; then if ! script_dir="$(dirname -- "$_shellvaculib_arg0_canonicalized")"; then
_shellvaculib_debug_print "svl_probably_in_script_dir failed to call dirname \$_shellvaculib_arg0_canonicalized, returning 1" _shellvaculib_debug_print 'svl_probably_in_script_dir failed to call $(dirname -- $_shellvaculib_arg0_canonicalized), returning 1'
return 1 return 1
fi fi
if ! canon_pwd="$(realpath -- "$PWD")"; then if ! canon_pwd="$(realpath -- "$PWD")"; then
_shellvaculib_debug_print "svl_probably_in_script_dir failed to call realpath \$PWD, returning 1" _shellvaculib_debug_print 'svl_probably_in_script_dir failed to call $(realpath -- $PWD), returning 1'
return 1 return 1
fi fi
[[ "$script_dir" == "$canon_pwd" ]] [[ $script_dir == "$canon_pwd" ]]
} }
svl_assert_probably_in_script_dir() { svl_assert_probably_in_script_dir() {
# shellcheck disable=SC2310
if ! svl_probably_in_script_dir; then if ! svl_probably_in_script_dir; then
svl_die "This script must be run in its directory" svl_die "This script must be run in its directory"
fi fi
@@ -263,10 +268,10 @@ svl_assert_probably_in_script_dir() {
} }
svl_assert_root() { svl_assert_root() {
if [[ "${EUID:-}" ]]; then if [[ -n ${EUID:-} ]]; then
svl_throw "\$EUID unset!?" svl_throw '$EUID unset!?'
fi fi
if [[ "$EUID" != 0 ]]; then if [[ $EUID != 0 ]]; then
svl_die "must be root to run this" svl_die "must be root to run this"
fi fi
return 0 return 0
@@ -280,7 +285,7 @@ svl_in_array() {
shift 1 shift 1
for value; do for value; do
if [[ "$value" == "$needle" ]]; then if [[ $value == "$needle" ]]; then
return 0 return 0
fi fi
done done
@@ -299,7 +304,7 @@ svl_confirm_or_die() {
fi fi
full_prompt="$prompt [yes/N]: " full_prompt="$prompt [yes/N]: "
read -r -p "$full_prompt" response || true read -r -p "$full_prompt" response || true
if [[ "$response" == "yes" ]]; then if [[ $response == "yes" ]]; then
return 0 return 0
else else
svl_die "exiting" svl_die "exiting"
@@ -314,6 +319,8 @@ svl_count() {
return 0 return 0
} }
# svl_count_matches 'foo*bar'
# counts the number of matches (correctly, even if nullglob is not set) and print to stdout
svl_count_matches() { svl_count_matches() {
svl_exact_args $# 1 svl_exact_args $# 1
declare nullglob_before declare nullglob_before
@@ -323,8 +330,10 @@ svl_count_matches() {
nullglob_before=disabled nullglob_before=disabled
fi fi
shopt -s nullglob shopt -s nullglob
# intentional expansion of arg, so that *s and such will expand
# shellcheck disable=SC2086
svl_count $1 svl_count $1
if [[ "$nullglob_before" == "disabled" ]]; then if [[ $nullglob_before == "disabled" ]]; then
shopt -u nullglob shopt -u nullglob
fi fi
return 0 return 0

View File

@@ -6,12 +6,13 @@
}: }:
let let
k = 1000; k = 1000;
m = k*1000; m = k * 1000;
g = m*1000; g = m * 1000;
t = g*1000; t = g * 1000;
ki = 1024; ki = 1024;
_8ki = 8*ki; _8ki = 8 * ki;
vdev_size = lib.pipe (14*t) [ # 14TB vdev_size = lib.pipe (14 * t) [
# 14TB
(n: n * 0.99) # leave 1% unused (n: n * 0.99) # leave 1% unused
builtins.floor # convert back to int builtins.floor # convert back to int
(n: (n / _8ki) * _8ki) # round to multiple of 8KiB (n: (n / _8ki) * _8ki) # round to multiple of 8KiB
@@ -39,29 +40,26 @@ let
easystore_2 easystore_2
]; ];
fs_type_raid = "fd00"; # Linux RAID fs_type_raid = "fd00"; # Linux RAID
fs_type_zfs = "a504"; # FreeBSD ZFS fs_type_zfs = "a504"; # FreeBSD ZFS
split_config = split_config = md_name: {
md_name: type = "disk";
{ content = {
type = "disk"; type = "gpt";
content = { partitions.mdadm = {
type = "gpt"; size = "${builtins.toString md_partition_size_ki}K";
partitions.mdadm = { type = fs_type_raid;
size = "${builtins.toString md_partition_size_ki}K"; content = {
type = fs_type_raid; type = "mdraid";
content = { name = md_name;
type = "mdraid";
name = md_name;
};
}; };
}; };
} };
; };
md_name_seagate = "propdata-seagates-combiner"; md_name_seagate = "propdata-seagates-combiner";
md_name_easystore = "propdata-easystores-combiner"; md_name_easystore = "propdata-easystores-combiner";
poolname = "propdata"; poolname = "propdata";
# each 14TB # each 14TB
easystores_14 = vaculib.listOfLines {} '' easystores_14 = vaculib.listOfLines { } ''
ata-WDC_WD140EDFZ-11A0VA0_Y5J3929C ata-WDC_WD140EDFZ-11A0VA0_Y5J3929C
ata-WDC_WD140EDGZ-11B2DA2_2BHRSN3F ata-WDC_WD140EDGZ-11B2DA2_2BHRSN3F
ata-WDC_WD140EDGZ-11B2DA2_2CG19ERP ata-WDC_WD140EDGZ-11B2DA2_2CG19ERP
@@ -112,7 +110,7 @@ in
easystores_10 easystores_10
easystores_14 easystores_14
seagate_gaming seagate_gaming
; ;
}; };
config.disko.enableConfig = false; config.disko.enableConfig = false;
config.disko.checkScripts = true; config.disko.checkScripts = true;

View File

@@ -7,12 +7,16 @@ svl_assert_root
declare top_dataset="trip" snapshot_name snapshot_prefix declare top_dataset="trip" snapshot_name snapshot_prefix
declare -a previous_snapshots declare -a previous_snapshots
mapfile -t previous_snapshots < <(zfs list -t snapshot --json "$top_dataset" | jq -r '.datasets|values[]|.snapshot_name') declare snapshot_lines
snapshot_lines="$(zfs list -t snapshot --json "$top_dataset" | jq -r '.datasets|values[]|.snapshot_name')"
mapfile -t previous_snapshots <<<"$snapshot_lines"
snapshot_prefix="semiauto--$(date '+%Y-%m-%d')--" snapshot_prefix="semiauto--$(date '+%Y-%m-%d')--"
declare -i idx=1 declare -i idx=1
while true; do while true; do
snapshot_name="${snapshot_prefix}${idx}" snapshot_name="${snapshot_prefix}${idx}"
# the only purpose of svl_in_array is to be used in a condition
# shellcheck disable=SC2310
if ! svl_in_array "$snapshot_name" "${previous_snapshots[@]}"; then if ! svl_in_array "$snapshot_name" "${previous_snapshots[@]}"; then
break break
fi fi
@@ -20,17 +24,18 @@ while true; do
done done
declare -a all_datasets declare -a all_datasets
declare all_datasets_lines
mapfile -t all_datasets < <(zfs list -r --json "$top_dataset" | jq -r '.datasets|keys[]') all_datasets_lines="$(zfs list -r --json "$top_dataset" | jq -r '.datasets|keys[]')"
mapfile -t all_datasets <<<"$all_datasets_lines"
declare -a should_snap_datasets excluded declare -a should_snap_datasets excluded
for dataset in "${all_datasets[@]}"; do for dataset in "${all_datasets[@]}"; do
case "$dataset" in case "$dataset" in
trip/fw-backup|trip/fw-backup/*|trip/fw-backup-2|trip/fw-backup-2/*) trip/fw-backup | trip/fw-backup/* | trip/fw-backup-2 | trip/fw-backup-2/*)
excluded+=("$dataset") excluded+=("$dataset")
;; ;;
*) *)
should_snap_datasets+=("$dataset") should_snap_datasets+=("$dataset")
;; ;;
esac esac
done done
@@ -41,7 +46,7 @@ done
echo echo
echo echo
read -r -p "Type y to continue: " confirmation read -r -p "Type y to continue: " confirmation
if [[ "$confirmation" != [yY] ]]; then if [[ $confirmation != [yY] ]]; then
echo "abort" echo "abort"
exit 1 exit 1
fi fi

View File

@@ -2,6 +2,7 @@
let let
shellFiles = [ shellFiles = [
"*.sh" "*.sh"
"*.bash"
"dcd" "dcd"
"dliam" "dliam"
"dmmm" "dmmm"
@@ -24,7 +25,7 @@ in
"--norc" "--norc"
"--source-path=${pkgs.shellvaculib}/bin" "--source-path=${pkgs.shellvaculib}/bin"
"--enable=all" "--enable=all"
"--exclude=SC2250" "--exclude=SC2250,SC2016"
]; ];
programs.shfmt.enable = true; programs.shfmt.enable = true;
programs.shfmt.includes = shellFiles; programs.shfmt.includes = shellFiles;