From 8e4220e6c6e937099384740b36343d1704571870 Mon Sep 17 00:00:00 2001 From: MaxCan-Code <35848843+MaxCan-Code@users.noreply.github.com> Date: Sat, 27 Aug 2022 02:37:16 +0000 Subject: [PATCH] flake: add templates (#2892) Add example flakes from `docs/nix-flakes.adoc` as templates. --- docs/nix-flakes.adoc | 14 ++++++++++++++ flake.nix | 17 +++++++++++++++++ templates/nix-darwin/flake.nix | 31 +++++++++++++++++++++++++++++++ templates/nixos/flake.nix | 29 +++++++++++++++++++++++++++++ templates/standalone/flake.nix | 29 +++++++++++++++++++++++++++++ 5 files changed, 120 insertions(+) create mode 100644 templates/nix-darwin/flake.nix create mode 100644 templates/nixos/flake.nix create mode 100644 templates/standalone/flake.nix diff --git a/docs/nix-flakes.adoc b/docs/nix-flakes.adoc index 487dde448..34b8b0f05 100644 --- a/docs/nix-flakes.adoc +++ b/docs/nix-flakes.adoc @@ -84,6 +84,10 @@ and `nixpkgs` url to `github:NixOS/nixpkgs/nixos-22.05`. * The Home Manager library is exported by the flake under `lib.hm`. + +* You can use the above `flake.nix` as a template in `~/.config/nixpkgs` by +[source,console] +$ nix flake new ~/.config/nixpkgs -t github:nix-community/home-manager ==== 2. Install Home Manager and apply the configuration by @@ -170,6 +174,11 @@ The Home Manager configuration is then part of the NixOS configuration and is automatically rebuilt with the system when using the appropriate command for the system, such as `nixos-rebuild switch --flake `. +You can use the above `flake.nix` as a template in `/etc/nixos` by + +[source,console] +$ nix flake new /etc/nixos -t github:nix-community/home-manager#nixos + [[sec-flakes-nix-darwin-module]] === nix-darwin module @@ -213,3 +222,8 @@ is similar to that of NixOS. The `flake.nix` would be: and it is also rebuilt with the nix-darwin generations. The rebuild command here may be `darwin-rebuild switch --flake `. + +You can use the above `flake.nix` as a template in `~/.config/darwin` by + +[source,console] +$ nix flake new ~/.config/darwin -t github:nix-community/home-manager#nix-darwin diff --git a/flake.nix b/flake.nix index c2885d624..a4e570e98 100644 --- a/flake.nix +++ b/flake.nix @@ -20,6 +20,23 @@ # unofficial; deprecated in Nix 2.8 darwinModule = self.darwinModules.default; + templates = { + standalone = { + path = ./templates/standalone; + description = "Standalone setup"; + }; + nixos = { + path = ./templates/nixos; + description = "Home Manager as a NixOS module,"; + }; + nix-darwin = { + path = ./templates/nix-darwin; + description = "Home Manager as a nix-darwin module,"; + }; + }; + + defaultTemplate = self.templates.standalone; + lib = { hm = (import ./modules/lib/stdlib-extended.nix nixpkgs.lib).hm; homeManagerConfiguration = { modules ? [ ], pkgs, lib ? pkgs.lib diff --git a/templates/nix-darwin/flake.nix b/templates/nix-darwin/flake.nix new file mode 100644 index 000000000..85b1db417 --- /dev/null +++ b/templates/nix-darwin/flake.nix @@ -0,0 +1,31 @@ +{ + description = "NixOS configuration"; + + inputs = { + nixpkgs.url = "github:nixos/nixpkgs/nixos-unstable"; + darwin.url = "github:lnl7/nix-darwin"; + darwin.inputs.nixpkgs.follows = "nixpkgs"; + home-manager.url = "github:nix-community/home-manager"; + home-manager.inputs.nixpkgs.follows = "nixpkgs"; + }; + + outputs = inputs@{ nixpkgs, home-manager, darwin, ... }: { + darwinConfigurations = { + hostname = darwin.lib.darwinSystem { + system = "x86_64-darwin"; + modules = [ + ./configuration.nix + home-manager.darwinModules.home-manager + { + home-manager.useGlobalPkgs = true; + home-manager.useUserPackages = true; + home-manager.users.jdoe = import ./home.nix; + + # Optionally, use home-manager.extraSpecialArgs to pass + # arguments to home.nix + } + ]; + }; + }; + }; +} diff --git a/templates/nixos/flake.nix b/templates/nixos/flake.nix new file mode 100644 index 000000000..fd4d0cd47 --- /dev/null +++ b/templates/nixos/flake.nix @@ -0,0 +1,29 @@ +{ + description = "NixOS configuration"; + + inputs = { + nixpkgs.url = "github:nixos/nixpkgs/nixos-unstable"; + home-manager.url = "github:nix-community/home-manager"; + home-manager.inputs.nixpkgs.follows = "nixpkgs"; + }; + + outputs = inputs@{ nixpkgs, home-manager, ... }: { + nixosConfigurations = { + hostname = nixpkgs.lib.nixosSystem { + system = "x86_64-linux"; + modules = [ + ./configuration.nix + home-manager.nixosModules.home-manager + { + home-manager.useGlobalPkgs = true; + home-manager.useUserPackages = true; + home-manager.users.jdoe = import ./home.nix; + + # Optionally, use home-manager.extraSpecialArgs to pass + # arguments to home.nix + } + ]; + }; + }; + }; +} diff --git a/templates/standalone/flake.nix b/templates/standalone/flake.nix new file mode 100644 index 000000000..c35ca3fa2 --- /dev/null +++ b/templates/standalone/flake.nix @@ -0,0 +1,29 @@ +{ + description = "Home Manager configuration of Jane Doe"; + + inputs = { + # Specify the source of Home Manager and Nixpkgs. + nixpkgs.url = "github:nixos/nixpkgs/nixos-unstable"; + home-manager = { + url = "github:nix-community/home-manager"; + inputs.nixpkgs.follows = "nixpkgs"; + }; + }; + + outputs = { nixpkgs, home-manager, ... }: + let + system = "x86_64-linux"; + pkgs = nixpkgs.legacyPackages.${system}; + in { + homeConfigurations.jdoe = home-manager.lib.homeManagerConfiguration { + inherit pkgs; + + # Specify your home configuration modules here, for example, + # the path to your home.nix. + modules = [ ./home.nix ]; + + # Optionally use extraSpecialArgs + # to pass through arguments to home.nix + }; + }; +}