Refactor installation process and enhance configuration management; add locale detection and autostart support
This commit is contained in:
+77
-41
@@ -1,18 +1,78 @@
|
||||
import * as p from "@clack/prompts";
|
||||
import { groups, packages, toDirs } from "./packages.js";
|
||||
import { detectGpuVendors, driverPackagesFor } from "./gpu.js";
|
||||
import { detectGpuVendors, driverPackagesFor, type GpuVendor } from "./gpu.js";
|
||||
import {
|
||||
backupConflicts,
|
||||
ensureSddmEnabled,
|
||||
ensureSudoSession,
|
||||
ensureYay,
|
||||
ensureZsh,
|
||||
ensureZshIsDefaultShell,
|
||||
installPackages,
|
||||
run,
|
||||
stow,
|
||||
writeAutostartConfig,
|
||||
writeLocaleConfig,
|
||||
writeNvidiaEnvConfig,
|
||||
writeState,
|
||||
} from "./stow.js";
|
||||
|
||||
const CANCELLED = Symbol("cancelled");
|
||||
|
||||
async function selectGroupPackages(chosen: Set<string>): Promise<typeof CANCELLED | void> {
|
||||
for (const group of groups) {
|
||||
const groupOptions = packages.filter((pkg) => pkg.group === group);
|
||||
const pick = await p.select({
|
||||
message: `Choose a ${group}`,
|
||||
options: groupOptions.map((pkg) => ({ value: pkg.name, label: pkg.name })),
|
||||
});
|
||||
if (p.isCancel(pick)) return CANCELLED;
|
||||
chosen.add(pick as string);
|
||||
}
|
||||
}
|
||||
|
||||
async function selectRecommendedPackages(chosen: Set<string>): Promise<typeof CANCELLED | void> {
|
||||
const ungrouped = packages.filter((pkg) => pkg.tier === "recommended" && !pkg.group);
|
||||
const picks = await p.multiselect({
|
||||
message: "Choose recommended packages",
|
||||
options: ungrouped.map((pkg) => ({ value: pkg.name, label: pkg.name })),
|
||||
initialValues: ungrouped.map((pkg) => pkg.name),
|
||||
required: false,
|
||||
});
|
||||
if (p.isCancel(picks)) return CANCELLED;
|
||||
for (const name of picks as string[]) chosen.add(name);
|
||||
}
|
||||
|
||||
/** Returns the vendors whose driver packages the user kept (added to `chosen`), or CANCELLED. */
|
||||
async function selectGpuDriverPackages(chosen: Set<string>): Promise<GpuVendor[] | typeof CANCELLED> {
|
||||
const vendors = detectGpuVendors();
|
||||
const options = vendors.flatMap((vendor) =>
|
||||
driverPackagesFor([vendor]).map((pkg) => ({ value: pkg, label: `${vendor}: ${pkg}` })),
|
||||
);
|
||||
if (options.length === 0) return vendors;
|
||||
|
||||
const picked = await p.multiselect({
|
||||
message: "Detected GPU(s) — confirm driver packages to install",
|
||||
options,
|
||||
initialValues: options.map((o) => o.value),
|
||||
required: false,
|
||||
});
|
||||
if (p.isCancel(picked)) return CANCELLED;
|
||||
|
||||
for (const name of picked) chosen.add(name);
|
||||
return vendors.filter((vendor) => driverPackagesFor([vendor]).some((pkg) => picked.includes(pkg)));
|
||||
}
|
||||
|
||||
async function maybeReboot(yes: boolean) {
|
||||
if (yes) {
|
||||
p.log.info("Rebooting now...");
|
||||
run("sudo", ["reboot"]);
|
||||
return;
|
||||
}
|
||||
const reboot = await p.confirm({ message: "Reboot now to apply changes?" });
|
||||
if (!p.isCancel(reboot) && reboot) run("sudo", ["reboot"]);
|
||||
}
|
||||
|
||||
export async function install({ yes = false }: { yes?: boolean } = {}) {
|
||||
p.intro("Hyprland dotfiles install");
|
||||
|
||||
@@ -23,42 +83,10 @@ export async function install({ yes = false }: { yes?: boolean } = {}) {
|
||||
|
||||
const chosen = new Set(packages.filter((pkg) => pkg.tier === "required").map((pkg) => pkg.name));
|
||||
|
||||
for (const group of groups) {
|
||||
const groupOptions = packages.filter((pkg) => pkg.group === group);
|
||||
const pick = await p.select({
|
||||
message: `Choose a ${group}`,
|
||||
options: groupOptions.map((pkg) => ({ value: pkg.name, label: pkg.name })),
|
||||
});
|
||||
if (p.isCancel(pick)) return p.cancel("Install cancelled.");
|
||||
chosen.add(pick as string);
|
||||
}
|
||||
|
||||
const ungrouped = packages.filter((pkg) => pkg.tier === "recommended" && !pkg.group);
|
||||
const picks = await p.multiselect({
|
||||
message: "Choose recommended packages",
|
||||
options: ungrouped.map((pkg) => ({ value: pkg.name, label: pkg.name })),
|
||||
initialValues: ungrouped.map((pkg) => pkg.name),
|
||||
required: false,
|
||||
});
|
||||
if (p.isCancel(picks)) return p.cancel("Install cancelled.");
|
||||
for (const name of picks as string[]) chosen.add(name);
|
||||
|
||||
const gpuVendors = detectGpuVendors();
|
||||
const gpuOptions = gpuVendors.flatMap((vendor) =>
|
||||
driverPackagesFor([vendor]).map((pkg) => ({ value: pkg, label: `${vendor}: ${pkg}` })),
|
||||
);
|
||||
let gpuPicks: string[] = [];
|
||||
if (gpuOptions.length > 0) {
|
||||
const picked = await p.multiselect({
|
||||
message: "Detected GPU(s) — confirm driver packages to install",
|
||||
options: gpuOptions,
|
||||
initialValues: gpuOptions.map((o) => o.value),
|
||||
required: false,
|
||||
});
|
||||
if (p.isCancel(picked)) return p.cancel("Install cancelled.");
|
||||
gpuPicks = picked as string[];
|
||||
for (const name of gpuPicks) chosen.add(name);
|
||||
}
|
||||
if ((await selectGroupPackages(chosen)) === CANCELLED) return p.cancel("Install cancelled.");
|
||||
if ((await selectRecommendedPackages(chosen)) === CANCELLED) return p.cancel("Install cancelled.");
|
||||
const keptGpuVendors = await selectGpuDriverPackages(chosen);
|
||||
if (keptGpuVendors === CANCELLED) return p.cancel("Install cancelled.");
|
||||
|
||||
p.note([...chosen].join("\n"), "Packages to install");
|
||||
if (!yes) {
|
||||
@@ -72,12 +100,18 @@ export async function install({ yes = false }: { yes?: boolean } = {}) {
|
||||
|
||||
ensureSddmEnabled();
|
||||
|
||||
if (chosen.has("zsh")) ensureZsh();
|
||||
|
||||
if (gpuVendors.includes("nvidia") && driverPackagesFor(["nvidia"]).some((pkg) => gpuPicks.includes(pkg))) {
|
||||
writeNvidiaEnvConfig();
|
||||
if (chosen.has("zsh")) {
|
||||
ensureZsh();
|
||||
ensureZshIsDefaultShell();
|
||||
}
|
||||
|
||||
if (keptGpuVendors.includes("nvidia")) writeNvidiaEnvConfig();
|
||||
|
||||
const autostartCmds = ["waybar", "ags", "mako", "hyprpaper"].filter((name) => chosen.has(name));
|
||||
if (chosen.has("hyprlauncher")) autostartCmds.push("hyprlauncher -d");
|
||||
writeAutostartConfig(autostartCmds);
|
||||
writeLocaleConfig();
|
||||
|
||||
const dirs = toDirs([...chosen]);
|
||||
const backupDir = backupConflicts(dirs);
|
||||
if (backupDir) p.log.info(`Backed up conflicting files to ${backupDir}`);
|
||||
@@ -85,5 +119,7 @@ export async function install({ yes = false }: { yes?: boolean } = {}) {
|
||||
stow(dirs);
|
||||
writeState({ dirs });
|
||||
|
||||
await maybeReboot(yes);
|
||||
|
||||
p.outro("Done.");
|
||||
}
|
||||
|
||||
@@ -0,0 +1,18 @@
|
||||
import { execFileSync } from "node:child_process";
|
||||
|
||||
export function detectKeyboardLayout(): string {
|
||||
try {
|
||||
const output = execFileSync("localectl", ["status"], { encoding: "utf8" });
|
||||
const match = output.match(/X11 Layout:\s*(\S+)/);
|
||||
if (match) return match[1];
|
||||
} catch {
|
||||
// localectl unavailable — fall back below
|
||||
}
|
||||
// ponytail: no VC-keymap fallback or multi-layout support — good enough
|
||||
// for the common case of a machine with an X11 keymap already configured.
|
||||
return "us";
|
||||
}
|
||||
|
||||
export function detectLang(): string | null {
|
||||
return process.env.LANG ?? null;
|
||||
}
|
||||
+1
-2
@@ -16,8 +16,7 @@ export const packages: Pkg[] = [
|
||||
{ name: "ttf-0xproto-nerd", tier: "required" },
|
||||
{ name: "waybar", tier: "recommended", group: "bar" },
|
||||
{ name: "ags", tier: "recommended", group: "bar", aur: true },
|
||||
{ name: "wofi", tier: "recommended", group: "launcher" },
|
||||
{ name: "rofi-wayland", dir: "rofi", tier: "recommended", group: "launcher", aur: true },
|
||||
{ name: "hyprlauncher", tier: "recommended", group: "launcher" },
|
||||
{ name: "kitty", tier: "recommended" },
|
||||
{ name: "foot", tier: "recommended" },
|
||||
{ name: "mako", tier: "recommended" },
|
||||
|
||||
+57
-13
@@ -10,8 +10,9 @@ import {
|
||||
rmSync,
|
||||
writeFileSync,
|
||||
} from "node:fs";
|
||||
import { homedir, tmpdir } from "node:os";
|
||||
import { homedir, tmpdir, userInfo } from "node:os";
|
||||
import { join } from "node:path";
|
||||
import { detectKeyboardLayout, detectLang } from "./locale.js";
|
||||
|
||||
const REPO_ROOT = new URL("..", import.meta.url).pathname;
|
||||
const PACKAGES_DIR = join(REPO_ROOT, "packages");
|
||||
@@ -19,9 +20,12 @@ const STATE_DIR = join(homedir(), ".config", "dotfiles");
|
||||
const STATE_FILE = join(STATE_DIR, "state.json");
|
||||
const BACKUP_ROOT = join(homedir(), ".dotfiles-backup");
|
||||
|
||||
export function run(cmd: string, args: string[], options: { cwd?: string } = {}) {
|
||||
type RunOptions = { cwd?: string; env?: NodeJS.ProcessEnv; stdin?: "inherit" | "ignore" };
|
||||
|
||||
export function run(cmd: string, args: string[], options: RunOptions = {}) {
|
||||
const { stdin = "inherit", ...rest } = options;
|
||||
try {
|
||||
execFileSync(cmd, args, { stdio: "inherit", ...options });
|
||||
execFileSync(cmd, args, { stdio: [stdin, "inherit", "inherit"], ...rest });
|
||||
} catch (err) {
|
||||
if (err && typeof err === "object" && "code" in err && err.code === "ENOENT") {
|
||||
throw new Error(`Command not found: ${cmd}. Is it installed and on PATH?`);
|
||||
@@ -70,20 +74,34 @@ export function installPackages(names: string[]) {
|
||||
export function ensureZsh() {
|
||||
const omzDir = join(homedir(), ".oh-my-zsh");
|
||||
if (!existsSync(omzDir)) {
|
||||
run("sh", [
|
||||
"-c",
|
||||
'sh -c "$(curl -fsSL https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)" "" --unattended',
|
||||
]);
|
||||
run(
|
||||
"sh",
|
||||
[
|
||||
"-c",
|
||||
'sh -c "$(curl -fsSL https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)" "" --unattended',
|
||||
],
|
||||
{ stdin: "ignore", env: { ...process.env, CHSH: "no", RUNZSH: "no", KEEP_ZSHRC: "yes" } },
|
||||
);
|
||||
}
|
||||
const zinitDir = join(homedir(), ".local", "share", "zinit", "zinit.git");
|
||||
if (!existsSync(zinitDir)) {
|
||||
run("sh", [
|
||||
"-c",
|
||||
'sh -c "$(curl -fsSL https://raw.githubusercontent.com/zdharma-continuum/zinit/HEAD/scripts/install.sh)"',
|
||||
]);
|
||||
run(
|
||||
"sh",
|
||||
["-c", 'sh -c "$(curl -fsSL https://raw.githubusercontent.com/zdharma-continuum/zinit/HEAD/scripts/install.sh)"'],
|
||||
{ stdin: "ignore" },
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
export function ensureZshIsDefaultShell() {
|
||||
// Arch's zsh package always installs to /usr/bin/zsh and registers exactly that path in
|
||||
// /etc/shells; `which zsh` can resolve to the /usr/sbin symlink instead (usr-merge), which
|
||||
// doesn't literally match /etc/shells and makes chsh warn spuriously.
|
||||
const zshPath = "/usr/bin/zsh";
|
||||
if (process.env.SHELL === zshPath) return;
|
||||
run("sudo", ["chsh", "-s", zshPath, userInfo().username]);
|
||||
}
|
||||
|
||||
export function ensureSddmEnabled() {
|
||||
try {
|
||||
execFileSync("systemctl", ["is-enabled", "--quiet", "sddm"]);
|
||||
@@ -92,9 +110,16 @@ export function ensureSddmEnabled() {
|
||||
}
|
||||
}
|
||||
|
||||
// Machine-generated, not stowed — only written when install detects an NVIDIA GPU.
|
||||
// Machine-generated Hyprland config lives under ~/.config/hypr/conf.d, sourced from the
|
||||
// stowed hyprland.lua via pcall(require, "conf.d.<name>") — never stowed itself, since it
|
||||
// varies per machine/per install run (chosen packages, detected hardware/locale).
|
||||
function hyprConfD(): string {
|
||||
return join(homedir(), ".config", "hypr", "conf.d");
|
||||
}
|
||||
|
||||
// Only written when install detects an NVIDIA GPU.
|
||||
export function writeNvidiaEnvConfig() {
|
||||
const dir = join(homedir(), ".config", "hypr", "conf.d");
|
||||
const dir = hyprConfD();
|
||||
mkdirSync(dir, { recursive: true });
|
||||
writeFileSync(
|
||||
join(dir, "nvidia.lua"),
|
||||
@@ -102,6 +127,25 @@ export function writeNvidiaEnvConfig() {
|
||||
);
|
||||
}
|
||||
|
||||
// Autostart list reflecting what was actually chosen (bar group pick, optional mako/hyprpaper) —
|
||||
// the static hyprland.lua no longer hardcodes this, so deselected/uninstalled apps are never exec'd.
|
||||
export function writeAutostartConfig(execCmds: string[]) {
|
||||
const dir = hyprConfD();
|
||||
mkdirSync(dir, { recursive: true });
|
||||
const body = execCmds.map((cmd) => ` hl.exec_cmd("${cmd}")`).join("\n");
|
||||
writeFileSync(join(dir, "autostart.lua"), `hl.on("hyprland.start", function()\n${body}\nend)\n`);
|
||||
}
|
||||
|
||||
export function writeLocaleConfig() {
|
||||
const dir = hyprConfD();
|
||||
mkdirSync(dir, { recursive: true });
|
||||
const layout = detectKeyboardLayout();
|
||||
const lang = detectLang();
|
||||
const lines = [`hl.config({ input = { kb_layout = "${layout}" } })`];
|
||||
if (lang) lines.push(`hl.env("LANG", "${lang}")`);
|
||||
writeFileSync(join(dir, "locale.lua"), lines.join("\n") + "\n");
|
||||
}
|
||||
|
||||
// Move real (non-symlink) files that would collide with a stow target out of the way.
|
||||
export function backupConflicts(dirs: string[]): string | null {
|
||||
const timestamp = new Date().toISOString().replace(/[:.]/g, "-");
|
||||
|
||||
Reference in New Issue
Block a user