2023-02-26 11:34:26 +00:00
|
|
|
import { Command } from "./dependencies.ts";
|
2023-06-07 13:43:02 +00:00
|
|
|
import { runAndReturnStdout, runCommand } from "./utilities.ts";
|
2023-02-07 10:21:46 +00:00
|
|
|
|
|
|
|
async function main(): Promise<void> {
|
|
|
|
const { options } = await new Command()
|
|
|
|
.name("copy-nixos-config")
|
|
|
|
.description(
|
|
|
|
'Copy NixOS configuration from "$BAUKE_DIR/nix/<hostname>/" to "/etc/nixos/"',
|
|
|
|
)
|
|
|
|
.option("--hostname", "The machine's configuration to copy.", {
|
2023-06-05 11:27:29 +00:00
|
|
|
default: (await runAndReturnStdout("hostname")).trim(),
|
2023-02-07 10:21:46 +00:00
|
|
|
})
|
2023-02-13 11:25:38 +00:00
|
|
|
.option("--diff", 'Output diffs between local and "/etc/nixos/" files.', {
|
|
|
|
standalone: true,
|
|
|
|
})
|
2023-02-13 11:24:12 +00:00
|
|
|
.option(
|
|
|
|
"--rebuild <rebuild:string>",
|
|
|
|
'Run "sudo nixos-rebuild <rebuild>" after copying.',
|
|
|
|
)
|
2023-02-07 10:21:46 +00:00
|
|
|
.parse(Deno.args);
|
|
|
|
|
|
|
|
const sourceDir = new URL(`../nix/${options.hostname}/`, import.meta.url);
|
|
|
|
const files = Array.from(Deno.readDirSync(sourceDir))
|
|
|
|
.filter((entry) => entry.name.endsWith(".nix"))
|
|
|
|
.map((entry) => sourceDir.pathname + entry.name);
|
|
|
|
|
2023-02-12 11:55:30 +00:00
|
|
|
if (options.diff) {
|
|
|
|
for (const file of files) {
|
|
|
|
const filename = file.slice(file.lastIndexOf("/") + 1);
|
2023-06-07 13:43:02 +00:00
|
|
|
await runCommand("delta", {
|
2023-06-05 11:27:29 +00:00
|
|
|
args: [`/etc/nixos/${filename}`, file],
|
2023-06-07 13:43:02 +00:00
|
|
|
});
|
2023-02-12 11:55:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-06-07 13:43:02 +00:00
|
|
|
await runCommand("sudo", {
|
2023-06-05 11:27:29 +00:00
|
|
|
args: [
|
2023-02-07 10:21:46 +00:00
|
|
|
"cp",
|
|
|
|
"--preserve=timestamps",
|
|
|
|
"--verbose",
|
|
|
|
...files,
|
|
|
|
"/etc/nixos/",
|
|
|
|
],
|
2023-06-07 13:43:02 +00:00
|
|
|
});
|
2023-02-13 11:24:12 +00:00
|
|
|
|
|
|
|
if (options.rebuild) {
|
2023-06-07 13:43:02 +00:00
|
|
|
await runCommand("sudo", {
|
2023-06-05 11:27:29 +00:00
|
|
|
args: ["nixos-rebuild", options.rebuild],
|
2023-06-07 13:43:02 +00:00
|
|
|
});
|
2023-02-13 11:24:12 +00:00
|
|
|
}
|
2023-02-07 10:21:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (import.meta.main) {
|
|
|
|
void main();
|
|
|
|
}
|