initial commit
Signed-off-by: Wolfgang <moe@notthebe.ee>
This commit is contained in:
commit
7c8c97fab3
14 changed files with 782 additions and 0 deletions
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
|
|
@ -0,0 +1 @@
|
|||
*~
|
||||
202
LICENSE
Normal file
202
LICENSE
Normal file
|
|
@ -0,0 +1,202 @@
|
|||
|
||||
Apache License
|
||||
Version 2.0, January 2004
|
||||
http://www.apache.org/licenses/
|
||||
|
||||
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
||||
|
||||
1. Definitions.
|
||||
|
||||
"License" shall mean the terms and conditions for use, reproduction,
|
||||
and distribution as defined by Sections 1 through 9 of this document.
|
||||
|
||||
"Licensor" shall mean the copyright owner or entity authorized by
|
||||
the copyright owner that is granting the License.
|
||||
|
||||
"Legal Entity" shall mean the union of the acting entity and all
|
||||
other entities that control, are controlled by, or are under common
|
||||
control with that entity. For the purposes of this definition,
|
||||
"control" means (i) the power, direct or indirect, to cause the
|
||||
direction or management of such entity, whether by contract or
|
||||
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
||||
outstanding shares, or (iii) beneficial ownership of such entity.
|
||||
|
||||
"You" (or "Your") shall mean an individual or Legal Entity
|
||||
exercising permissions granted by this License.
|
||||
|
||||
"Source" form shall mean the preferred form for making modifications,
|
||||
including but not limited to software source code, documentation
|
||||
source, and configuration files.
|
||||
|
||||
"Object" form shall mean any form resulting from mechanical
|
||||
transformation or translation of a Source form, including but
|
||||
not limited to compiled object code, generated documentation,
|
||||
and conversions to other media types.
|
||||
|
||||
"Work" shall mean the work of authorship, whether in Source or
|
||||
Object form, made available under the License, as indicated by a
|
||||
copyright notice that is included in or attached to the work
|
||||
(an example is provided in the Appendix below).
|
||||
|
||||
"Derivative Works" shall mean any work, whether in Source or Object
|
||||
form, that is based on (or derived from) the Work and for which the
|
||||
editorial revisions, annotations, elaborations, or other modifications
|
||||
represent, as a whole, an original work of authorship. For the purposes
|
||||
of this License, Derivative Works shall not include works that remain
|
||||
separable from, or merely link (or bind by name) to the interfaces of,
|
||||
the Work and Derivative Works thereof.
|
||||
|
||||
"Contribution" shall mean any work of authorship, including
|
||||
the original version of the Work and any modifications or additions
|
||||
to that Work or Derivative Works thereof, that is intentionally
|
||||
submitted to Licensor for inclusion in the Work by the copyright owner
|
||||
or by an individual or Legal Entity authorized to submit on behalf of
|
||||
the copyright owner. For the purposes of this definition, "submitted"
|
||||
means any form of electronic, verbal, or written communication sent
|
||||
to the Licensor or its representatives, including but not limited to
|
||||
communication on electronic mailing lists, source code control systems,
|
||||
and issue tracking systems that are managed by, or on behalf of, the
|
||||
Licensor for the purpose of discussing and improving the Work, but
|
||||
excluding communication that is conspicuously marked or otherwise
|
||||
designated in writing by the copyright owner as "Not a Contribution."
|
||||
|
||||
"Contributor" shall mean Licensor and any individual or Legal Entity
|
||||
on behalf of whom a Contribution has been received by Licensor and
|
||||
subsequently incorporated within the Work.
|
||||
|
||||
2. Grant of Copyright License. Subject to the terms and conditions of
|
||||
this License, each Contributor hereby grants to You a perpetual,
|
||||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||
copyright license to reproduce, prepare Derivative Works of,
|
||||
publicly display, publicly perform, sublicense, and distribute the
|
||||
Work and such Derivative Works in Source or Object form.
|
||||
|
||||
3. Grant of Patent License. Subject to the terms and conditions of
|
||||
this License, each Contributor hereby grants to You a perpetual,
|
||||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||
(except as stated in this section) patent license to make, have made,
|
||||
use, offer to sell, sell, import, and otherwise transfer the Work,
|
||||
where such license applies only to those patent claims licensable
|
||||
by such Contributor that are necessarily infringed by their
|
||||
Contribution(s) alone or by combination of their Contribution(s)
|
||||
with the Work to which such Contribution(s) was submitted. If You
|
||||
institute patent litigation against any entity (including a
|
||||
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
||||
or a Contribution incorporated within the Work constitutes direct
|
||||
or contributory patent infringement, then any patent licenses
|
||||
granted to You under this License for that Work shall terminate
|
||||
as of the date such litigation is filed.
|
||||
|
||||
4. Redistribution. You may reproduce and distribute copies of the
|
||||
Work or Derivative Works thereof in any medium, with or without
|
||||
modifications, and in Source or Object form, provided that You
|
||||
meet the following conditions:
|
||||
|
||||
(a) You must give any other recipients of the Work or
|
||||
Derivative Works a copy of this License; and
|
||||
|
||||
(b) You must cause any modified files to carry prominent notices
|
||||
stating that You changed the files; and
|
||||
|
||||
(c) You must retain, in the Source form of any Derivative Works
|
||||
that You distribute, all copyright, patent, trademark, and
|
||||
attribution notices from the Source form of the Work,
|
||||
excluding those notices that do not pertain to any part of
|
||||
the Derivative Works; and
|
||||
|
||||
(d) If the Work includes a "NOTICE" text file as part of its
|
||||
distribution, then any Derivative Works that You distribute must
|
||||
include a readable copy of the attribution notices contained
|
||||
within such NOTICE file, excluding those notices that do not
|
||||
pertain to any part of the Derivative Works, in at least one
|
||||
of the following places: within a NOTICE text file distributed
|
||||
as part of the Derivative Works; within the Source form or
|
||||
documentation, if provided along with the Derivative Works; or,
|
||||
within a display generated by the Derivative Works, if and
|
||||
wherever such third-party notices normally appear. The contents
|
||||
of the NOTICE file are for informational purposes only and
|
||||
do not modify the License. You may add Your own attribution
|
||||
notices within Derivative Works that You distribute, alongside
|
||||
or as an addendum to the NOTICE text from the Work, provided
|
||||
that such additional attribution notices cannot be construed
|
||||
as modifying the License.
|
||||
|
||||
You may add Your own copyright statement to Your modifications and
|
||||
may provide additional or different license terms and conditions
|
||||
for use, reproduction, or distribution of Your modifications, or
|
||||
for any such Derivative Works as a whole, provided Your use,
|
||||
reproduction, and distribution of the Work otherwise complies with
|
||||
the conditions stated in this License.
|
||||
|
||||
5. Submission of Contributions. Unless You explicitly state otherwise,
|
||||
any Contribution intentionally submitted for inclusion in the Work
|
||||
by You to the Licensor shall be under the terms and conditions of
|
||||
this License, without any additional terms or conditions.
|
||||
Notwithstanding the above, nothing herein shall supersede or modify
|
||||
the terms of any separate license agreement you may have executed
|
||||
with Licensor regarding such Contributions.
|
||||
|
||||
6. Trademarks. This License does not grant permission to use the trade
|
||||
names, trademarks, service marks, or product names of the Licensor,
|
||||
except as required for reasonable and customary use in describing the
|
||||
origin of the Work and reproducing the content of the NOTICE file.
|
||||
|
||||
7. Disclaimer of Warranty. Unless required by applicable law or
|
||||
agreed to in writing, Licensor provides the Work (and each
|
||||
Contributor provides its Contributions) on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
||||
implied, including, without limitation, any warranties or conditions
|
||||
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
||||
PARTICULAR PURPOSE. You are solely responsible for determining the
|
||||
appropriateness of using or redistributing the Work and assume any
|
||||
risks associated with Your exercise of permissions under this License.
|
||||
|
||||
8. Limitation of Liability. In no event and under no legal theory,
|
||||
whether in tort (including negligence), contract, or otherwise,
|
||||
unless required by applicable law (such as deliberate and grossly
|
||||
negligent acts) or agreed to in writing, shall any Contributor be
|
||||
liable to You for damages, including any direct, indirect, special,
|
||||
incidental, or consequential damages of any character arising as a
|
||||
result of this License or out of the use or inability to use the
|
||||
Work (including but not limited to damages for loss of goodwill,
|
||||
work stoppage, computer failure or malfunction, or any and all
|
||||
other commercial damages or losses), even if such Contributor
|
||||
has been advised of the possibility of such damages.
|
||||
|
||||
9. Accepting Warranty or Additional Liability. While redistributing
|
||||
the Work or Derivative Works thereof, You may choose to offer,
|
||||
and charge a fee for, acceptance of support, warranty, indemnity,
|
||||
or other liability obligations and/or rights consistent with this
|
||||
License. However, in accepting such obligations, You may act only
|
||||
on Your own behalf and on Your sole responsibility, not on behalf
|
||||
of any other Contributor, and only if You agree to indemnify,
|
||||
defend, and hold each Contributor harmless for any liability
|
||||
incurred by, or claims asserted against, such Contributor by reason
|
||||
of your accepting any such warranty or additional liability.
|
||||
|
||||
END OF TERMS AND CONDITIONS
|
||||
|
||||
APPENDIX: How to apply the Apache License to your work.
|
||||
|
||||
To apply the Apache License to your work, attach the following
|
||||
boilerplate notice, with the fields enclosed by brackets "[]"
|
||||
replaced with your own identifying information. (Don't include
|
||||
the brackets!) The text should be enclosed in the appropriate
|
||||
comment syntax for the file format. We also recommend that a
|
||||
file or class name and description of purpose be included on the
|
||||
same "printed page" as the copyright notice for easier
|
||||
identification within third-party archives.
|
||||
|
||||
Copyright [yyyy] [name of copyright owner]
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
24
README.org
Normal file
24
README.org
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
* Minimal NixOS Root on ZFS configuration
|
||||
This repo contains a minimal set of configuration needed for
|
||||
installing NixOS on a computer with ZFS as root filesystem.
|
||||
|
||||
#+begin_src text
|
||||
.gitignore
|
||||
LICENSE
|
||||
configuration.nix <- configuration shared by all hosts
|
||||
flake.lock
|
||||
flake.nix
|
||||
hosts/exampleHost/default.nix <- per-host configuration
|
||||
hosts/exampleHost/sshUnlock.txt
|
||||
modules/boot/default.nix
|
||||
modules/default.nix
|
||||
modules/fileSystems/default.nix
|
||||
modules/networking/default.nix
|
||||
modules/users/default.nix
|
||||
#+end_src
|
||||
|
||||
Just enough to get you started.
|
||||
|
||||
See [[https://github.com/ne9z/personal-dotfiles][my personal dotfiles repo]] for an example of how to configure
|
||||
servers and desktop computers in the same git repository, based on
|
||||
this template repo you are viewing now.
|
||||
59
configuration.nix
Normal file
59
configuration.nix
Normal file
|
|
@ -0,0 +1,59 @@
|
|||
{ my-config, zfs-root, inputs, pkgs, lib, ... }: {
|
||||
# load module config to top-level configuration
|
||||
inherit my-config zfs-root;
|
||||
|
||||
# Let 'nixos-version --json' know about the Git revision
|
||||
# of this flake.
|
||||
system.configurationRevision = if (inputs.self ? rev) then
|
||||
inputs.self.rev
|
||||
else
|
||||
throw "refuse to build: git tree is dirty";
|
||||
|
||||
system.stateVersion = "22.11";
|
||||
|
||||
# Enable NetworkManager for wireless networking,
|
||||
# You can configure networking with "nmtui" command.
|
||||
networking.useDHCP = true;
|
||||
networking.networkmanager.enable = false;
|
||||
|
||||
users.users = {
|
||||
root = {
|
||||
initialHashedPassword = "rootHash_placeholder";
|
||||
openssh.authorizedKeys.keys = [ "sshKey_placeholder" ];
|
||||
};
|
||||
};
|
||||
|
||||
programs.neovim = {
|
||||
enable = true;
|
||||
viAlias = true;
|
||||
vimAlias = true;
|
||||
};
|
||||
|
||||
imports = [
|
||||
"${inputs.nixpkgs}/nixos/modules/installer/scan/not-detected.nix"
|
||||
# "${inputs.nixpkgs}/nixos/modules/profiles/qemu-guest.nix"
|
||||
];
|
||||
|
||||
services.openssh = {
|
||||
enable = lib.mkDefault true;
|
||||
settings = { PasswordAuthentication = lib.mkDefault false; };
|
||||
};
|
||||
|
||||
boot.zfs.forceImportRoot = lib.mkDefault false;
|
||||
|
||||
nix.settings.experimental-features = lib.mkDefault [ "nix-command" "flakes" ];
|
||||
|
||||
programs.git.enable = true;
|
||||
|
||||
security = {
|
||||
doas.enable = lib.mkDefault true;
|
||||
sudo.enable = lib.mkDefault false;
|
||||
};
|
||||
|
||||
environment.systemPackages = builtins.attrValues {
|
||||
inherit (pkgs)
|
||||
mg # emacs-like editor
|
||||
jq # other programs
|
||||
;
|
||||
};
|
||||
}
|
||||
27
flake.lock
generated
Normal file
27
flake.lock
generated
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
{
|
||||
"nodes": {
|
||||
"nixpkgs": {
|
||||
"locked": {
|
||||
"lastModified": 1685533922,
|
||||
"narHash": "sha256-y4FCQpYafMQ42l1V+NUrMel9RtFtZo59PzdzflKR/lo=",
|
||||
"owner": "nixos",
|
||||
"repo": "nixpkgs",
|
||||
"rev": "3a70dd92993182f8e514700ccf5b1ae9fc8a3b8d",
|
||||
"type": "github"
|
||||
},
|
||||
"original": {
|
||||
"owner": "nixos",
|
||||
"ref": "nixos-23.05",
|
||||
"repo": "nixpkgs",
|
||||
"type": "github"
|
||||
}
|
||||
},
|
||||
"root": {
|
||||
"inputs": {
|
||||
"nixpkgs": "nixpkgs"
|
||||
}
|
||||
}
|
||||
},
|
||||
"root": "root",
|
||||
"version": 7
|
||||
}
|
||||
27
flake.nix
Normal file
27
flake.nix
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
{
|
||||
inputs.nixpkgs.url = "github:nixos/nixpkgs/nixos-23.05";
|
||||
## after reboot, you can track rolling release by using
|
||||
#inputs.nixpkgs.url = "github:nixos/nixpkgs/nixos-unstable";
|
||||
|
||||
outputs = { self, nixpkgs }@inputs:
|
||||
let
|
||||
lib = nixpkgs.lib;
|
||||
mkHost = { my-config, zfs-root, pkgs, system, ... }:
|
||||
lib.nixosSystem {
|
||||
inherit system;
|
||||
modules = [
|
||||
./modules
|
||||
(import ./configuration.nix {
|
||||
inherit my-config zfs-root inputs pkgs lib;
|
||||
})
|
||||
];
|
||||
};
|
||||
in {
|
||||
nixosConfigurations = {
|
||||
exampleHost = let
|
||||
system = "x86_64-linux";
|
||||
pkgs = nixpkgs.legacyPackages.${system};
|
||||
in mkHost (import ./hosts/exampleHost { inherit system pkgs; });
|
||||
};
|
||||
};
|
||||
}
|
||||
45
hosts/exampleHost/changeHostName.txt
Normal file
45
hosts/exampleHost/changeHostName.txt
Normal file
|
|
@ -0,0 +1,45 @@
|
|||
How to set hostname for a host.
|
||||
|
||||
This repo is structured in a way that allows managing multiple hosts
|
||||
in a single repo. As a consequence of this, hostname appears in
|
||||
several different places and should be updated together if needed.
|
||||
|
||||
The entry point of this repo is ``flake.nix'' file, which takes a
|
||||
revision of the nixpkgs as input then gives an attribute set (attrset)
|
||||
as output:
|
||||
|
||||
{
|
||||
nixosConfigurations = {
|
||||
exampleHost = { ... };
|
||||
host2 = { ... };
|
||||
host3 = { ... };
|
||||
...
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
The hostname specified here inside nixosConfigurations attrset is used
|
||||
when ``nix command'' is invoked, as in
|
||||
|
||||
nixos-install --flake ./#exampleHost
|
||||
|
||||
The hostname also appears in the filesystem path of the per-host
|
||||
configuration file:
|
||||
|
||||
hosts/exampleHost/default.nix
|
||||
hosts/host2/default.nix
|
||||
...
|
||||
|
||||
Finally, the hostname is specified in the zfs-boot.networking.hostName
|
||||
option inside per-host configuration file at
|
||||
hosts/exampleHost/default.nix:
|
||||
|
||||
zfs-boot.networking.hostName = "exampleHost";
|
||||
|
||||
If the value of this option is identical to the name of
|
||||
nixosConfigurations.<hostname> attrset, all subsequent invocations of
|
||||
``nix command'' will default to this hostname, i.e. the command:
|
||||
|
||||
nixos-rebuild boot
|
||||
|
||||
will automatically append ``#hostName'' when evaluating the flake.
|
||||
37
hosts/exampleHost/default.nix
Normal file
37
hosts/exampleHost/default.nix
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
# #
|
||||
##
|
||||
## per-host configuration for exampleHost
|
||||
##
|
||||
##
|
||||
|
||||
{ system, pkgs, ... }: {
|
||||
inherit pkgs system;
|
||||
zfs-root = {
|
||||
boot = {
|
||||
devNodes = "/dev/disk/by-id/";
|
||||
bootDevices = [ "bootDevices_placeholder" ];
|
||||
immutable = false;
|
||||
availableKernelModules = [ "kernelModules_placeholder" ];
|
||||
removableEfi = true;
|
||||
kernelParams = [ ];
|
||||
sshUnlock = {
|
||||
# read sshUnlock.txt file.
|
||||
enable = false;
|
||||
authorizedKeys = [ ];
|
||||
};
|
||||
};
|
||||
networking = {
|
||||
# read changeHostName.txt file.
|
||||
hostName = "exampleHost";
|
||||
timeZone = "Europe/Berlin";
|
||||
hostId = "abcd1234";
|
||||
};
|
||||
};
|
||||
|
||||
# To add more options to per-host configuration, you can create a
|
||||
# custom configuration module, then add it here.
|
||||
my-config = {
|
||||
# Enable custom gnome desktop on exampleHost
|
||||
template.desktop.gnome.enable = false;
|
||||
};
|
||||
}
|
||||
61
hosts/exampleHost/sshUnlock.txt
Normal file
61
hosts/exampleHost/sshUnlock.txt
Normal file
|
|
@ -0,0 +1,61 @@
|
|||
Audience
|
||||
|
||||
This text is intended for users who wish to remotely unlock their
|
||||
natively encrypted ZFS root filesystem with SSH during the early
|
||||
stages of system boot up.
|
||||
|
||||
=================================================================
|
||||
|
||||
Steps
|
||||
|
||||
By default, the initial RAM disk image, (hereafter refered to as
|
||||
initrd) only includes essential kernel modules needed for mounting the
|
||||
root filesystem.
|
||||
|
||||
To enable network connection during this stage of system boot, you
|
||||
need to instruct NixOS to include kernel modules needed for the
|
||||
network adapter.
|
||||
|
||||
Check which module is needed with:
|
||||
|
||||
nix-shell -p pciutils --run "lspci -v | grep -iA8 'network\|ethernet'"
|
||||
|
||||
Then, add the the modules to
|
||||
|
||||
zfs-root.boot.availableKernelModules
|
||||
|
||||
option inside the per-host configuration file. This file is located
|
||||
at hosts/exampleHost/default.nix.
|
||||
|
||||
By default, initrd uses DHCP auto-configuration for networking. If
|
||||
you need to specify a static IP address, see the documentation for
|
||||
"ip=" kernel command line option for NFS root:
|
||||
|
||||
https://www.kernel.org/doc/Documentation/filesystems/nfs/nfsroot.txt
|
||||
|
||||
Then, add the necessary kernel command line options to
|
||||
|
||||
zfs-root.boot.kernelParams
|
||||
|
||||
option inside the per-host configuration file.
|
||||
|
||||
Finally, you need to generate a pair of SSH private keys exclusively
|
||||
for use with decrypting the root filesystem during boot. This pair of
|
||||
keys should not be used anywhere else. Reasons for this can be found
|
||||
in the documentation NixOS option:
|
||||
|
||||
boot.initrd.network.ssh.hostKeys
|
||||
|
||||
> Do NOT use your regular SSH host private keys for this purpose or
|
||||
> you'll expose them to regular users! Additionally, even if your initrd
|
||||
> supports secrets, if you're using initrd SSH to unlock an encrypted
|
||||
> disk then using your regular host keys exposes the private keys on
|
||||
> your unencrypted boot partition.
|
||||
|
||||
You can generate host key with:
|
||||
|
||||
ssh-keygen -t ed25519 -N "" -f "/var/lib/ssh_unlock_zfs_ed25519_key"
|
||||
ssh-keygen -t rsa -N "" -f "/var/lib/ssh_unlock_zfs_rsa_key"
|
||||
|
||||
If you are performing a system installation, change path from
|
||||
/var/lib to /mnt/var/lib in both commands.
|
||||
162
modules/boot/default.nix
Normal file
162
modules/boot/default.nix
Normal file
|
|
@ -0,0 +1,162 @@
|
|||
{ config, lib, pkgs, ... }:
|
||||
|
||||
let
|
||||
cfg = config.zfs-root.boot;
|
||||
inherit (lib) mkIf types mkDefault mkOption mkMerge strings;
|
||||
inherit (builtins) head toString map tail;
|
||||
in {
|
||||
options.zfs-root.boot = {
|
||||
enable = mkOption {
|
||||
description = "Enable root on ZFS support";
|
||||
type = types.bool;
|
||||
default = true;
|
||||
};
|
||||
devNodes = mkOption {
|
||||
description = "Specify where to discover ZFS pools";
|
||||
type = types.str;
|
||||
apply = x:
|
||||
assert (strings.hasSuffix "/" x
|
||||
|| abort "devNodes '${x}' must have trailing slash!");
|
||||
x;
|
||||
default = "/dev/disk/by-id/";
|
||||
};
|
||||
bootDevices = mkOption {
|
||||
description = "Specify boot devices";
|
||||
type = types.nonEmptyListOf types.str;
|
||||
};
|
||||
availableKernelModules = mkOption {
|
||||
type = types.nonEmptyListOf types.str;
|
||||
default = [ "uas" "nvme" "ahci" ];
|
||||
};
|
||||
kernelParams = mkOption {
|
||||
type = types.listOf types.str;
|
||||
default = [ ];
|
||||
};
|
||||
immutable = mkOption {
|
||||
description = "Enable root on ZFS immutable root support";
|
||||
type = types.bool;
|
||||
default = false;
|
||||
};
|
||||
removableEfi = mkOption {
|
||||
description = "install bootloader to fallback location";
|
||||
type = types.bool;
|
||||
default = true;
|
||||
};
|
||||
partitionScheme = mkOption {
|
||||
default = {
|
||||
biosBoot = "-part5";
|
||||
efiBoot = "-part1";
|
||||
swap = "-part4";
|
||||
bootPool = "-part2";
|
||||
rootPool = "-part3";
|
||||
};
|
||||
description = "Describe on disk partitions";
|
||||
type = types.attrsOf types.str;
|
||||
};
|
||||
sshUnlock = {
|
||||
enable = mkOption {
|
||||
type = types.bool;
|
||||
default = false;
|
||||
};
|
||||
authorizedKeys = mkOption {
|
||||
type = types.listOf types.str;
|
||||
default = [ ];
|
||||
};
|
||||
};
|
||||
};
|
||||
config = mkIf (cfg.enable) (mkMerge [
|
||||
{
|
||||
zfs-root.fileSystems.datasets = {
|
||||
"rpool/nixos/home" = mkDefault "/home";
|
||||
"rpool/nixos/var/lib" = mkDefault "/var/lib";
|
||||
"rpool/nixos/var/log" = mkDefault "/var/log";
|
||||
"bpool/nixos/root" = "/boot";
|
||||
};
|
||||
}
|
||||
(mkIf (!cfg.immutable) {
|
||||
zfs-root.fileSystems.datasets = { "rpool/nixos/root" = "/"; };
|
||||
})
|
||||
(mkIf cfg.immutable {
|
||||
zfs-root.fileSystems = {
|
||||
datasets = {
|
||||
"rpool/nixos/empty" = "/";
|
||||
"rpool/nixos/root" = "/oldroot";
|
||||
};
|
||||
bindmounts = {
|
||||
"/oldroot/nix" = "/nix";
|
||||
"/oldroot/etc/nixos" = "/etc/nixos";
|
||||
};
|
||||
};
|
||||
boot.initrd.postDeviceCommands = ''
|
||||
if ! grep -q zfs_no_rollback /proc/cmdline; then
|
||||
zpool import -N rpool
|
||||
zfs rollback -r rpool/nixos/empty@start
|
||||
zpool export -a
|
||||
fi
|
||||
'';
|
||||
})
|
||||
{
|
||||
zfs-root.fileSystems = {
|
||||
efiSystemPartitions =
|
||||
(map (diskName: diskName + cfg.partitionScheme.efiBoot)
|
||||
cfg.bootDevices);
|
||||
swapPartitions =
|
||||
(map (diskName: diskName + cfg.partitionScheme.swap) cfg.bootDevices);
|
||||
};
|
||||
boot = {
|
||||
kernelPackages =
|
||||
mkDefault config.boot.zfs.package.latestCompatibleLinuxPackages;
|
||||
initrd.availableKernelModules = cfg.availableKernelModules;
|
||||
kernelParams = cfg.kernelParams;
|
||||
supportedFilesystems = [ "zfs" ];
|
||||
zfs = {
|
||||
devNodes = cfg.devNodes;
|
||||
forceImportRoot = mkDefault false;
|
||||
};
|
||||
loader = {
|
||||
efi = {
|
||||
canTouchEfiVariables = (if cfg.removableEfi then false else true);
|
||||
efiSysMountPoint = ("/boot/efis/" + (head cfg.bootDevices)
|
||||
+ cfg.partitionScheme.efiBoot);
|
||||
};
|
||||
generationsDir.copyKernels = true;
|
||||
grub = {
|
||||
enable = true;
|
||||
devices = (map (diskName: cfg.devNodes + diskName) cfg.bootDevices);
|
||||
efiInstallAsRemovable = cfg.removableEfi;
|
||||
copyKernels = true;
|
||||
efiSupport = true;
|
||||
zfsSupport = true;
|
||||
extraInstallCommands = (toString (map (diskName: ''
|
||||
set -x
|
||||
${pkgs.coreutils-full}/bin/cp -r ${config.boot.loader.efi.efiSysMountPoint}/EFI /boot/efis/${diskName}${cfg.partitionScheme.efiBoot}
|
||||
set +x
|
||||
'') (tail cfg.bootDevices)));
|
||||
};
|
||||
};
|
||||
};
|
||||
}
|
||||
(mkIf cfg.sshUnlock.enable {
|
||||
boot.initrd = {
|
||||
network = {
|
||||
enable = true;
|
||||
ssh = {
|
||||
enable = true;
|
||||
hostKeys = [
|
||||
"/var/lib/ssh_unlock_zfs_ed25519_key"
|
||||
"/var/lib/ssh_unlock_zfs_rsa_key"
|
||||
];
|
||||
authorizedKeys = cfg.sshUnlock.authorizedKeys;
|
||||
};
|
||||
postCommands = ''
|
||||
tee -a /root/.profile >/dev/null <<EOF
|
||||
if zfs load-key rpool/nixos; then
|
||||
pkill zfs
|
||||
fi
|
||||
exit
|
||||
EOF'';
|
||||
};
|
||||
};
|
||||
})
|
||||
]);
|
||||
}
|
||||
3
modules/default.nix
Normal file
3
modules/default.nix
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
{ config, lib, pkgs, ... }: {
|
||||
imports = [ ./boot ./fileSystems ./networking ./my-config ];
|
||||
}
|
||||
64
modules/fileSystems/default.nix
Normal file
64
modules/fileSystems/default.nix
Normal file
|
|
@ -0,0 +1,64 @@
|
|||
{ config, lib, ... }:
|
||||
|
||||
let
|
||||
cfg = config.zfs-root.fileSystems;
|
||||
inherit (lib) mkIf types mkDefault mkOption mkMerge mapAttrsToList;
|
||||
in {
|
||||
options.zfs-root.fileSystems = {
|
||||
datasets = mkOption {
|
||||
description = "Set mountpoint for datasets";
|
||||
type = types.attrsOf types.str;
|
||||
default = { };
|
||||
};
|
||||
bindmounts = mkOption {
|
||||
description = "Set mountpoint for bindmounts";
|
||||
type = types.attrsOf types.str;
|
||||
default = { };
|
||||
};
|
||||
efiSystemPartitions = mkOption {
|
||||
description = "Set mountpoint for efi system partitions";
|
||||
type = types.listOf types.str;
|
||||
default = [ ];
|
||||
};
|
||||
swapPartitions = mkOption {
|
||||
description = "Set swap partitions";
|
||||
type = types.listOf types.str;
|
||||
default = [ ];
|
||||
};
|
||||
};
|
||||
config.fileSystems = mkMerge (mapAttrsToList (dataset: mountpoint: {
|
||||
"${mountpoint}" = {
|
||||
device = "${dataset}";
|
||||
fsType = "zfs";
|
||||
options = [ "X-mount.mkdir" "noatime" ];
|
||||
neededForBoot = true;
|
||||
};
|
||||
}) cfg.datasets ++ mapAttrsToList (bindsrc: mountpoint: {
|
||||
"${mountpoint}" = {
|
||||
device = "${bindsrc}";
|
||||
fsType = "none";
|
||||
options = [ "bind" "X-mount.mkdir" "noatime" ];
|
||||
};
|
||||
}) cfg.bindmounts ++ map (esp: {
|
||||
"/boot/efis/${esp}" = {
|
||||
device = "${config.zfs-root.boot.devNodes}/${esp}";
|
||||
fsType = "vfat";
|
||||
options = [
|
||||
"x-systemd.idle-timeout=1min"
|
||||
"x-systemd.automount"
|
||||
"noauto"
|
||||
"nofail"
|
||||
"noatime"
|
||||
"X-mount.mkdir"
|
||||
];
|
||||
};
|
||||
}) cfg.efiSystemPartitions);
|
||||
config.swapDevices = mkDefault (map (swap: {
|
||||
device = "${config.zfs-root.boot.devNodes}/${swap}";
|
||||
discardPolicy = mkDefault "both";
|
||||
randomEncryption = {
|
||||
enable = true;
|
||||
allowDiscards = mkDefault true;
|
||||
};
|
||||
}) cfg.swapPartitions);
|
||||
}
|
||||
41
modules/my-config/default.nix
Normal file
41
modules/my-config/default.nix
Normal file
|
|
@ -0,0 +1,41 @@
|
|||
{ config, lib, pkgs, ... }:
|
||||
let inherit (lib) types mkIf mkDefault mkOption;
|
||||
in {
|
||||
options.my-config = {
|
||||
template.desktop.gnome.enable = mkOption {
|
||||
description = "Enable my customized GNOME desktop";
|
||||
type = types.bool;
|
||||
default = false;
|
||||
};
|
||||
};
|
||||
|
||||
# if my-config.template.desktop.gnome.enable is set to true
|
||||
# set the following options
|
||||
config = mkIf config.my-config.template.desktop.gnome.enable {
|
||||
services.xserver = {
|
||||
enable = true;
|
||||
desktopManager.gnome.enable = true;
|
||||
displayManager.gdm.enable = true;
|
||||
};
|
||||
|
||||
# GNOME must be used with a non-root user,
|
||||
# add a non-root user
|
||||
users.users.alice = {
|
||||
# Generate hashed password with "mkpasswd -m sha-512" command,
|
||||
# "!" disables login.
|
||||
# "mkpasswd" without "-m sha-512" will not work
|
||||
initialHashedPassword = "!";
|
||||
description = "Full Name";
|
||||
# Users in "wheel" group are allowed to use "doas" command
|
||||
# to obtain root permissions.
|
||||
extraGroups = [ "wheel" ];
|
||||
packages = builtins.attrValues {
|
||||
inherit (pkgs)
|
||||
mg # emacs-like editor
|
||||
jq # other programs
|
||||
;
|
||||
};
|
||||
isNormalUser = true;
|
||||
};
|
||||
};
|
||||
}
|
||||
29
modules/networking/default.nix
Normal file
29
modules/networking/default.nix
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
{ config, lib, ... }:
|
||||
let
|
||||
cfg = config.zfs-root.networking;
|
||||
inherit (lib) types mkDefault mkOption;
|
||||
in {
|
||||
options.zfs-root.networking = {
|
||||
hostName = mkOption {
|
||||
description = "The name of the machine. Used by nix flake.";
|
||||
type = types.str;
|
||||
default = "exampleHost";
|
||||
};
|
||||
timeZone = mkOption {
|
||||
type = types.str;
|
||||
default = "Etc/UTC";
|
||||
};
|
||||
hostId = mkOption {
|
||||
description = "Set host id";
|
||||
type = types.str;
|
||||
};
|
||||
};
|
||||
config = {
|
||||
networking.hostId = cfg.hostId;
|
||||
time.timeZone = cfg.timeZone;
|
||||
networking = {
|
||||
firewall.enable = mkDefault true;
|
||||
hostName = cfg.hostName;
|
||||
};
|
||||
};
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue