From 08bfaa4e88e80af6ca0e94444ffbb754dc6b030e Mon Sep 17 00:00:00 2001 From: Sridhar Ratnakumar Date: Sat, 3 Dec 2022 21:57:30 -0500 Subject: [PATCH] Isolate common nix into lib.nix --- activate.nix | 32 ---------------- flake.nix | 11 +++--- home/default.nix | 28 -------------- lib.nix | 87 ++++++++++++++++++++++++++++++++++++++++++ nix-darwin/default.nix | 10 +---- nixos/default.nix | 10 ----- 6 files changed, 93 insertions(+), 85 deletions(-) delete mode 100644 activate.nix create mode 100644 lib.nix diff --git a/activate.nix b/activate.nix deleted file mode 100644 index 54f6143..0000000 --- a/activate.nix +++ /dev/null @@ -1,32 +0,0 @@ -# A rough flake-parts module for activating the system -# -# TODO: Replace with deploy-rs or (new) nixinate -{ self, inputs, ... }: -{ - perSystem = { system, pkgs, lib, ... }: { - apps.default = - let - # Create a flake app that wraps the given bash CLI. - bashCmdApp = name: cmd: { - type = "app"; - program = - (pkgs.writeShellApplication { - inherit name; - text = '' - set -x - ${cmd} - ''; - }) + "/bin/${name}"; - }; - in - if system == "aarch64-darwin" then - bashCmdApp "darwin" '' - ${self.darwinConfigurations.default.system}/sw/bin/darwin-rebuild \ - switch --flake ${self}#default - '' - else - bashCmdApp "linux" '' - ${lib.getExe pkgs.nixos-rebuild} --use-remote-sudo switch -j auto - ''; - }; -} diff --git a/flake.nix b/flake.nix index 68b70e3..e6525c2 100644 --- a/flake.nix +++ b/flake.nix @@ -33,11 +33,11 @@ inputs.flake-parts.lib.mkFlake { inherit (inputs) self; } { systems = [ "x86_64-linux" "aarch64-darwin" ]; imports = [ + ./lib.nix ./config.nix ./home ./nixos ./nix-darwin - ./activate.nix ]; people = { @@ -68,11 +68,9 @@ ]; }; }; - # Configurations for my only[^1] macOS machine (using nix-darwin) - # - # [^1]: This is why attr key is 'default'. + # Configurations for my (only) macOS machine (using nix-darwin) darwinConfigurations = { - default = self.lib-darwin.mkMacosSystem { + default = self.lib.mkMacosSystem { imports = [ self.darwinModules.default # Defined in nix-darwin/default.nix ./systems/darwin.nix @@ -81,11 +79,12 @@ }; }; - perSystem = { pkgs, ... }: { + perSystem = { pkgs, config, ... }: { devShells.default = pkgs.mkShell { buildInputs = [ pkgs.nixpkgs-fmt ]; }; formatter = pkgs.nixpkgs-fmt; + apps.default = config.apps.activate; }; }; } diff --git a/home/default.nix b/home/default.nix index 2baede9..7b940e5 100644 --- a/home/default.nix +++ b/home/default.nix @@ -33,33 +33,5 @@ ]; }; }; - nixosModules.home-manager = { - imports = [ - inputs.home-manager.nixosModules.home-manager - ({ - home-manager.useGlobalPkgs = true; - home-manager.useUserPackages = true; - home-manager.extraSpecialArgs = { - inherit inputs; - system = "x86_64-linux"; - flake = { inherit config; }; - }; - }) - ]; - }; - darwinModules.home-manager = { - imports = [ - inputs.home-manager.darwinModules.home-manager - ({ - home-manager.useGlobalPkgs = true; - home-manager.useUserPackages = true; - home-manager.extraSpecialArgs = { - inherit inputs; - system = "aarch64-darwin"; - flake = { inherit config; }; - }; - }) - ]; - }; }; } diff --git a/lib.nix b/lib.nix new file mode 100644 index 0000000..8838dd0 --- /dev/null +++ b/lib.nix @@ -0,0 +1,87 @@ +# Support code for this repo. This module could be made its own external repo. +{ self, inputs, config, ... }: +{ + flake = { + # Linux home-manager module + nixosModules.home-manager = { + imports = [ + inputs.home-manager.nixosModules.home-manager + ({ + home-manager.useGlobalPkgs = true; + home-manager.useUserPackages = true; + home-manager.extraSpecialArgs = { + inherit inputs; + system = "x86_64-linux"; + flake = { inherit config; }; + }; + }) + ]; + }; + # macOS home-manager module + darwinModules.home-manager = { + imports = [ + inputs.home-manager.darwinModules.home-manager + ({ + home-manager.useGlobalPkgs = true; + home-manager.useUserPackages = true; + home-manager.extraSpecialArgs = { + inherit inputs; + system = "aarch64-darwin"; + flake = { inherit config; }; + }; + }) + ]; + }; + lib = { + mkLinuxSystem = mod: inputs.nixpkgs.lib.nixosSystem rec { + system = "x86_64-linux"; + # Arguments to pass to all modules. + specialArgs = { + inherit system inputs; + flake = { inherit config; }; + }; + modules = [ mod ]; + }; + + mkMacosSystem = mod: inputs.darwin.lib.darwinSystem rec { + system = "aarch64-darwin"; + specialArgs = { + inherit inputs system; + flake = { inherit config; }; + rosettaPkgs = import inputs.nixpkgs { system = "x86_64-darwin"; }; + }; + modules = [ mod ]; + }; + }; + }; + + perSystem = { system, pkgs, lib, ... }: { + # A rough app for activating the system locally. + # + # TODO: Replace with deploy-rs or (new) nixinate + apps.activate = + let + # Create a flake app that wraps the given bash CLI. + bashCmdApp = name: cmd: { + type = "app"; + program = + (pkgs.writeShellApplication { + inherit name; + text = '' + set -x + ${cmd} + ''; + }) + "/bin/${name}"; + }; + in + if system == "aarch64-darwin" then + bashCmdApp "darwin" '' + ${self.darwinConfigurations.default.system}/sw/bin/darwin-rebuild \ + switch --flake ${self}#default + '' + else + bashCmdApp "linux" '' + ${lib.getExe pkgs.nixos-rebuild} --use-remote-sudo switch -j auto + ''; + }; +} diff --git a/nix-darwin/default.nix b/nix-darwin/default.nix index df7be80..96bd955 100644 --- a/nix-darwin/default.nix +++ b/nix-darwin/default.nix @@ -18,14 +18,6 @@ ../nixos/caches ]; }; - lib-darwin.mkMacosSystem = mod: inputs.darwin.lib.darwinSystem rec { - system = "aarch64-darwin"; - specialArgs = { - inherit inputs system; - flake = { inherit config; }; - rosettaPkgs = import inputs.nixpkgs { system = "x86_64-darwin"; }; - }; - modules = [ mod ]; - }; + }; } diff --git a/nixos/default.nix b/nixos/default.nix index c4d1325..319ec7a 100644 --- a/nixos/default.nix +++ b/nixos/default.nix @@ -30,15 +30,5 @@ in ./current-location.nix ]; }; - - lib.mkLinuxSystem = mod: inputs.nixpkgs.lib.nixosSystem rec { - system = "x86_64-linux"; - # Arguments to pass to all modules. - specialArgs = { - inherit system inputs; - flake = { inherit config; }; - }; - modules = [ mod ]; - }; }; }