cleanup
This commit is contained in:
parent
e4838c1afe
commit
1fcbbd8655
3 changed files with 0 additions and 305 deletions
|
|
@ -1,155 +0,0 @@
|
||||||
#!/usr/bin/env bash
|
|
||||||
set -euo pipefail
|
|
||||||
|
|
||||||
ROOT="${1:-../Modules}" # root dir (defaults to Modules)
|
|
||||||
|
|
||||||
OUT_DIR="generated-examples"
|
|
||||||
mkdir -p "$OUT_DIR"
|
|
||||||
|
|
||||||
OUT1="$OUT_DIR/example-with-headers-and-comments.nix"
|
|
||||||
OUT2="$OUT_DIR/example-with-headers.nix"
|
|
||||||
OUT3="$OUT_DIR/example-with-comments.nix"
|
|
||||||
OUT4="$OUT_DIR/example.nix"
|
|
||||||
|
|
||||||
echo "Generating $OUT1, $OUT2, $OUT3, $OUT4 from modules under $ROOT ..."
|
|
||||||
|
|
||||||
# --- Extract top-of-file module description ---
|
|
||||||
extract_module_description() {
|
|
||||||
local file="$1"
|
|
||||||
awk '
|
|
||||||
/^ *#/ { gsub(/^ *# */, ""); print " # " $0 }
|
|
||||||
/^\{ *config,/ { exit }
|
|
||||||
' "$file"
|
|
||||||
}
|
|
||||||
|
|
||||||
# --- Extract options from a module ---
|
|
||||||
extract_options() {
|
|
||||||
local file="$1"
|
|
||||||
local with_comments="$2"
|
|
||||||
|
|
||||||
grep -nE "^[[:space:]]*[a-zA-Z0-9_-]+[[:space:]]*=" "$file" \
|
|
||||||
| grep -E "lib\.mkOption|lib\.mkEnableOption" \
|
|
||||||
| sed -E 's/^([0-9]+):[[:space:]]*([a-zA-Z0-9_-]+)[[:space:]]*=.*/\1 \2/' \
|
|
||||||
| while read -r line name; do
|
|
||||||
block="$(tail -n +$((line+1)) "$file" | awk '/};/ {exit} {print}')"
|
|
||||||
defline="$(sed -n "${line}p" "$file")"
|
|
||||||
|
|
||||||
kind="mkOption"
|
|
||||||
[[ "$defline" == *"mkEnableOption"* ]] && kind="mkEnableOption"
|
|
||||||
|
|
||||||
if [[ "$kind" == "mkEnableOption" ]]; then
|
|
||||||
[[ "$with_comments" == "yes" ]] && echo "# mkEnableOption (bool)"
|
|
||||||
echo "${name} = true;"
|
|
||||||
else
|
|
||||||
otype="$(echo "$block" | grep -E "type[[:space:]]*=" | head -n1 | sed -E 's/.*type[[:space:]]*=[[:space:]]*//; s/;//')"
|
|
||||||
value="…"
|
|
||||||
desc=""
|
|
||||||
|
|
||||||
if echo "$block" | grep -q "default[[:space:]]*="; then
|
|
||||||
value="$(echo "$block" | grep "default[[:space:]]*=" | head -n1 | sed -E 's/.*default[[:space:]]*=[[:space:]]*(.*);/\1/')"
|
|
||||||
elif echo "$block" | grep -q "example[[:space:]]*="; then
|
|
||||||
value="$(echo "$block" | grep "example[[:space:]]*=" | head -n1 | sed -E 's/.*example[[:space:]]*=[[:space:]]*(.*);/\1/')"
|
|
||||||
fi
|
|
||||||
|
|
||||||
if echo "$block" | grep -q "description[[:space:]]*="; then
|
|
||||||
desc="$(echo "$block" | grep "description[[:space:]]*=" | head -n1 | sed -E 's/.*description[[:space:]]*=[[:space:]]*//; s/^"//; s/"$//')"
|
|
||||||
[[ -z "$value" || "$value" == "…" ]] && value="… # $desc"
|
|
||||||
fi
|
|
||||||
|
|
||||||
if [[ "$with_comments" == "yes" ]]; then
|
|
||||||
[[ -n "$otype" ]] && echo "# mkOption type=$otype" || echo "# mkOption"
|
|
||||||
[[ -n "$desc" ]] && echo "# $desc"
|
|
||||||
fi
|
|
||||||
echo "${name} = ${value};"
|
|
||||||
fi
|
|
||||||
echo
|
|
||||||
done
|
|
||||||
}
|
|
||||||
|
|
||||||
# --- Extract module header (optional) ---
|
|
||||||
extract_header() {
|
|
||||||
local file="$1"
|
|
||||||
awk '
|
|
||||||
/^ *\{ *config,/ { exit }
|
|
||||||
/^$/ { next }
|
|
||||||
{ print " " $0 }
|
|
||||||
' "$file"
|
|
||||||
}
|
|
||||||
|
|
||||||
# --- Write prolog ---
|
|
||||||
write_prolog() {
|
|
||||||
local file="$1"
|
|
||||||
{
|
|
||||||
echo "{ config, lib, pkgs, ... }:"
|
|
||||||
echo
|
|
||||||
echo "{"
|
|
||||||
echo " nyx-module = {"
|
|
||||||
} > "$file"
|
|
||||||
}
|
|
||||||
|
|
||||||
# --- Write epilog ---
|
|
||||||
write_epilog() {
|
|
||||||
local file="$1"
|
|
||||||
{
|
|
||||||
echo " };"
|
|
||||||
echo "}"
|
|
||||||
} >> "$file"
|
|
||||||
}
|
|
||||||
|
|
||||||
# --- Prepare all output files ---
|
|
||||||
for f in "$OUT1" "$OUT2" "$OUT3" "$OUT4"; do
|
|
||||||
write_prolog "$f"
|
|
||||||
done
|
|
||||||
|
|
||||||
# --- Append modules for a section ---
|
|
||||||
append_modules() {
|
|
||||||
local base="$1"
|
|
||||||
local with_headers="$2"
|
|
||||||
local with_comments="$3"
|
|
||||||
local subdir="$4"
|
|
||||||
|
|
||||||
find "$ROOT/$subdir" -type f -name "*.nix" ! -name "default.nix" | sort | while read -r f; do
|
|
||||||
module="$(basename "$f" .nix)"
|
|
||||||
{
|
|
||||||
# [[ "$with_headers" == "yes" ]] && extract_module_description "$f"
|
|
||||||
[[ "$with_headers" == "yes" ]] && extract_header "$f"
|
|
||||||
echo " ${module} = {"
|
|
||||||
extract_options "$f" "$with_comments" | sed 's/^/ /'
|
|
||||||
echo " };"
|
|
||||||
echo
|
|
||||||
} >> "$base"
|
|
||||||
done
|
|
||||||
}
|
|
||||||
|
|
||||||
# --- Process each section in order ---
|
|
||||||
for section in "system" "home" "hardware"; do
|
|
||||||
for f in "$OUT1" "$OUT2" "$OUT3" "$OUT4"; do
|
|
||||||
echo " ${section} = {" >> "$f"
|
|
||||||
done
|
|
||||||
|
|
||||||
append_modules "$OUT1" yes yes "${section^}"
|
|
||||||
append_modules "$OUT2" yes no "${section^}"
|
|
||||||
append_modules "$OUT3" no yes "${section^}"
|
|
||||||
append_modules "$OUT4" no no "${section^}"
|
|
||||||
|
|
||||||
for f in "$OUT1" "$OUT2" "$OUT3" "$OUT4"; do
|
|
||||||
echo " };" >> "$f"
|
|
||||||
echo >> "$f"
|
|
||||||
done
|
|
||||||
done
|
|
||||||
|
|
||||||
# --- Finish all files ---
|
|
||||||
for f in "$OUT1" "$OUT2" "$OUT3" "$OUT4"; do
|
|
||||||
write_epilog "$f"
|
|
||||||
done
|
|
||||||
|
|
||||||
echo "Wrote:"
|
|
||||||
echo " - $OUT1"
|
|
||||||
echo " - $OUT2"
|
|
||||||
echo " - $OUT3"
|
|
||||||
echo " - $OUT4"
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
# a
|
|
||||||
|
|
@ -1,127 +0,0 @@
|
||||||
#!/usr/bin/env bash
|
|
||||||
set -euo pipefail
|
|
||||||
|
|
||||||
ROOT="${1:-../Modules}" # pass root dir, defaults to Modules
|
|
||||||
|
|
||||||
# Generate module boilerplate for each .nix (excluding default.nix)
|
|
||||||
gen_module_boilerplate() {
|
|
||||||
local file="$1"
|
|
||||||
local module
|
|
||||||
module="$(basename "$file" .nix)"
|
|
||||||
|
|
||||||
# skip if non-empty
|
|
||||||
if [[ -s "$file" ]]; then
|
|
||||||
echo "Skipping non-empty $file"
|
|
||||||
return
|
|
||||||
fi
|
|
||||||
|
|
||||||
local ns target
|
|
||||||
if [[ "$file" == *"/Home/"* ]]; then
|
|
||||||
ns="home"
|
|
||||||
target="home.packages"
|
|
||||||
else
|
|
||||||
ns="system"
|
|
||||||
target="environment.systemPackages"
|
|
||||||
fi
|
|
||||||
|
|
||||||
cat > "$file" <<EOF
|
|
||||||
{ config, lib, pkgs, ... }:
|
|
||||||
|
|
||||||
let
|
|
||||||
cfg = config.nyx-module.${ns}.${module};
|
|
||||||
in
|
|
||||||
{
|
|
||||||
options.nyx-module.${ns}.${module} = {
|
|
||||||
enable = lib.mkEnableOption "Enable ${module} (${ns}) module";
|
|
||||||
|
|
||||||
package = lib.mkOption {
|
|
||||||
type = lib.types.package;
|
|
||||||
default = pkgs.${module};
|
|
||||||
description = "Package to install for ${module}.";
|
|
||||||
};
|
|
||||||
};
|
|
||||||
|
|
||||||
config = lib.mkIf cfg.enable {
|
|
||||||
${target} = [ cfg.package ];
|
|
||||||
};
|
|
||||||
}
|
|
||||||
EOF
|
|
||||||
echo "Wrote boilerplate for $file"
|
|
||||||
}
|
|
||||||
|
|
||||||
gen_default_for_dir() {
|
|
||||||
local dir="$1"
|
|
||||||
local file="${dir}/default.nix"
|
|
||||||
|
|
||||||
# collect non-default nix files in this directory
|
|
||||||
local local_modules=()
|
|
||||||
for f in "$dir"/*.nix; do
|
|
||||||
[[ -f "$f" && "$(basename "$f")" != "default.nix" ]] && local_modules+=("./$(basename "$f")")
|
|
||||||
done
|
|
||||||
|
|
||||||
# if no local nix files, collect subdirs that contain default.nix
|
|
||||||
if (( ${#local_modules[@]} > 0 )); then
|
|
||||||
new_imports=("${local_modules[@]}")
|
|
||||||
else
|
|
||||||
local subdirs=()
|
|
||||||
for sub in "$dir"/*/; do
|
|
||||||
[[ -d "$sub" && -f "$sub/default.nix" ]] && subdirs+=("./$(basename "$sub")")
|
|
||||||
done
|
|
||||||
new_imports=("${subdirs[@]}")
|
|
||||||
fi
|
|
||||||
|
|
||||||
mkdir -p "$dir"
|
|
||||||
|
|
||||||
if [[ -f "$file" ]]; then
|
|
||||||
# Replace imports block in place
|
|
||||||
awk -v n="${#new_imports[@]}" -v imports="$(printf '%s\n' "${new_imports[@]}")" '
|
|
||||||
BEGIN {
|
|
||||||
split(imports, arr, "\n")
|
|
||||||
printing=1
|
|
||||||
}
|
|
||||||
/imports = \[/ {
|
|
||||||
print " imports = ["
|
|
||||||
for (i=1; i<=n; i++) {
|
|
||||||
print " " arr[i]
|
|
||||||
}
|
|
||||||
print " ];"
|
|
||||||
# skip until closing bracket
|
|
||||||
while (getline > 0) {
|
|
||||||
if ($0 ~ /\];/) break
|
|
||||||
}
|
|
||||||
next
|
|
||||||
}
|
|
||||||
{ print }
|
|
||||||
' "$file" > "$file.tmp" && mv "$file.tmp" "$file"
|
|
||||||
echo "Updated imports in $file"
|
|
||||||
else
|
|
||||||
# brand new default.nix
|
|
||||||
{
|
|
||||||
echo "{ config, lib, pkgs, ... }:"
|
|
||||||
echo
|
|
||||||
echo "{"
|
|
||||||
echo " imports = ["
|
|
||||||
for imp in "${new_imports[@]}"; do
|
|
||||||
echo " $imp"
|
|
||||||
done
|
|
||||||
echo " ];"
|
|
||||||
echo "}"
|
|
||||||
} > "$file"
|
|
||||||
echo "Created $file"
|
|
||||||
fi
|
|
||||||
}
|
|
||||||
|
|
||||||
# Walk the tree
|
|
||||||
echo "Generating nix module boilerplates under $ROOT ..."
|
|
||||||
|
|
||||||
# 1. Generate boilerplate for all non-default modules
|
|
||||||
find "$ROOT" -type f -name "*.nix" ! -name "default.nix" | while read -r f; do
|
|
||||||
gen_module_boilerplate "$f"
|
|
||||||
done
|
|
||||||
|
|
||||||
# 2. Generate default.nix in every directory
|
|
||||||
find "$ROOT" -type d | while read -r d; do
|
|
||||||
gen_default_for_dir "$d"
|
|
||||||
done
|
|
||||||
|
|
||||||
echo "Done! All modules + defaults generated."
|
|
||||||
|
|
@ -1,23 +0,0 @@
|
||||||
############################################################
|
|
||||||
# System Modules
|
|
||||||
############################################################
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
############################################################
|
|
||||||
# Home Modules
|
|
||||||
############################################################
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue