From d4de691afb8cb2d458baf620bbe32411e5ad444b Mon Sep 17 00:00:00 2001 From: matthewcroughan Date: Mon, 3 Jan 2022 10:50:50 +0000 Subject: [PATCH] init --- README.md | 77 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ flake.lock | 27 +++++++++++++++++++ flake.nix | 47 +++++++++++++++++++++++++++++++++ 3 files changed, 151 insertions(+) create mode 100644 README.md create mode 100644 flake.lock create mode 100644 flake.nix diff --git a/README.md b/README.md new file mode 100644 index 0000000..b697773 --- /dev/null +++ b/README.md @@ -0,0 +1,77 @@ +# Nixinate 🕶️ + +Nixinate is a proof of concept that generates a deployment script for each +`nixosConfiguration` you already have in your flake, which can be ran via `nix +run`, thanks to the `apps` attribute of the [flake +schema](https://nixos.wiki/wiki/Flakes#Flake_schema). + +## Usage + +To add and configure `nixinate` in your own flake, you need to: + +1. Add the result of `nixinate self` to the `apps` attribute of your flake. +2. Add and configure `_module.args.nixinate` to the `nixosConfigurations` you want to deploy + +Below is a minimal example: + +```nix +{ + inputs = { + nixpkgs.url = "github:nixos/nixpkgs/nixos-21.11"; + nixinate.url = "github:matthewcroughan/nixinate"; + }; + + outputs = { self, nixpkgs, nixinate }: { + apps = nixinate.nixinate.x86_64-linux self; + nixosConfigurations = { + myMachine = nixpkgs.lib.nixosSystem { + modules = [ + (import ./my-configuration.nix) + { + _module.args.nixinate = { + host = "itchy.scratchy.com"; + sshUser = "matthew"; + }; + } + # ... other configuration ... + ]; + }; + }; + }; +} +``` + +Each `nixosConfiguration` you have configured should have a deployment script in +`apps.nixinate`, visible in `nix flake show` like this: + +``` +$ nix flake show +git+file:///etc/nixos +├───apps +│ └───nixinate +│ └───myMachine: app +└───nixosConfigurations + └───myMachine: NixOS configuration +``` + +To finally execute the deployment script, use `nix run .#apps.nixinate.myMachine` + +#### Example Run + +``` +[root@myMachine:/etc/nixos]# nix run .#apps.nixinate.myMachine +🚀 Deploying nixosConfigurations.myMachine from /nix/store/279p8aaclmng8kc3mdmrmi6q3n76r1i7-source +👤 SSH User: matthew +🌐 SSH Host: itchy.scratchy.com +🚀 Sending flake to myMachine via rsync: +(matthew@itchy.scratchy.com) Password: +🤞 Activating configuration on myMachine via ssh: +(matthew@itchy.scratchy.com) Password: +[sudo] password for matthew: +building the system configuration... +activating the configuration... +setting up /etc... +reloading user units for matthew... +setting up tmpfiles +Connection to itchy.scratchy.com closed. +``` diff --git a/flake.lock b/flake.lock new file mode 100644 index 0000000..0f219fa --- /dev/null +++ b/flake.lock @@ -0,0 +1,27 @@ +{ + "nodes": { + "nixpkgs": { + "locked": { + "lastModified": 1640887906, + "narHash": "sha256-Eupk1UlNicCD2UNZuEKt6yhE6kFWAxXM/HyziOjG9CA=", + "owner": "nixos", + "repo": "nixpkgs", + "rev": "8a053bc2255659c5ca52706b9e12e76a8f50dbdd", + "type": "github" + }, + "original": { + "owner": "nixos", + "ref": "nixos-21.11", + "repo": "nixpkgs", + "type": "github" + } + }, + "root": { + "inputs": { + "nixpkgs": "nixpkgs" + } + } + }, + "root": "root", + "version": 7 +} diff --git a/flake.nix b/flake.nix new file mode 100644 index 0000000..1d56423 --- /dev/null +++ b/flake.nix @@ -0,0 +1,47 @@ +{ + description = "Nixinate your systems 🕶️"; + inputs.nixpkgs.url = "github:nixos/nixpkgs/nixos-21.11"; + outputs = { self, nixpkgs, ... }: + let + version = builtins.substring 0 8 self.lastModifiedDate; + supportedSystems = [ "x86_64-linux" "x86_64-darwin" "aarch64-linux" "aarch64-darwin" ]; + forAllSystems = nixpkgs.lib.genAttrs supportedSystems; + nixpkgsFor = forAllSystems (system: import nixpkgs { inherit system; overlays = [ self.overlay ]; }); + in rec + { + overlay = final: prev: { + generateApps = flake: + let + machines = builtins.attrNames flake.nixosConfigurations; + validMachines = final.lib.remove "" (final.lib.forEach machines (x: final.lib.optionalString (flake.nixosConfigurations."${x}"._module.args ? nixinate) "${x}" )); + mkDeployScript = machine: final.writeScript "deploy-${machine}.sh" '' + set -e + SSH_USER=${flake.nixosConfigurations.${machine}._module.args.nixinate.sshUser} + SSH_HOST=${flake.nixosConfigurations.${machine}._module.args.nixinate.host} + + echo "🚀 Deploying nixosConfigurations.${machine} from ${flake}" + echo "👤 SSH User: $SSH_USER" + echo "🌐 SSH Host: $SSH_HOST" + echo "🚀 Sending flake to ${machine} via rsync:" + ( set -x; ${final.rsync}/bin/rsync -q -vz --recursive --zc=zstd ${flake}/* $SSH_USER@$SSH_HOST:/tmp/nixcfg/ ) + echo "🤞 Activating configuration on ${machine} via ssh:" + ( set -x; ${final.openssh}/bin/ssh -t $SSH_USER@$SSH_HOST 'sudo nixos-rebuild switch --flake /tmp/nixcfg#${machine}' ) + ''; + in + { + nixinate = + ( + nixpkgs.lib.genAttrs + validMachines + (x: + { + type = "app"; + program = toString (mkDeployScript x); + } + ) + ); + }; + }; + nixinate = forAllSystems (system: nixpkgsFor.${system}.generateApps); + }; +}