├── .envrc ├── .gitattributes ├── .gitignore ├── LICENSE ├── README.md ├── flake.lock ├── flake.nix ├── hosts ├── rpi3-001.nix ├── rpi3-002.nix ├── rpi4-001.nix ├── rpi4-002.nix └── rpi4-003.nix ├── lib ├── default.nix ├── define-host.nix ├── device-profiles.nix ├── keys │ ├── admin.pub │ └── deploy.pub └── make-image.nix ├── platforms ├── home-manager │ └── modules │ │ ├── default.nix │ │ └── lab │ │ ├── default.nix │ │ ├── presets │ │ ├── default.nix │ │ └── programs │ │ │ ├── default.nix │ │ │ ├── nushell │ │ │ ├── config.nu │ │ │ ├── default.nix │ │ │ └── env.nu │ │ │ └── starship.nix │ │ └── profiles │ │ ├── default.nix │ │ └── fancy-shell.nix └── nixos │ ├── doc │ └── default.nix │ ├── modules │ ├── default.nix │ └── lab │ │ ├── default.nix │ │ ├── filesystems │ │ ├── default.nix │ │ └── zfs │ │ │ ├── .envrc │ │ │ ├── .gitignore │ │ │ ├── default.nix │ │ │ ├── develop.nix │ │ │ ├── propctl.nu │ │ │ └── test_propctl.nu │ │ ├── host.nix │ │ ├── networks.nix │ │ ├── profiles │ │ ├── default.nix │ │ ├── file-server.nix │ │ ├── observability │ │ │ └── default.nix │ │ ├── router.nix │ │ └── vpn │ │ │ ├── client.nix │ │ │ ├── default.nix │ │ │ ├── server.nix │ │ │ └── vpn-tunnel-key.age │ │ ├── services │ │ ├── default.nix │ │ ├── dhcp │ │ │ ├── default.nix │ │ │ └── sync-records-to-etcd.nu │ │ ├── discovery │ │ │ ├── default.nix │ │ │ └── server.nix │ │ ├── dns.nix │ │ ├── gateway.nix │ │ └── vpn │ │ │ ├── default.nix │ │ │ └── server.nix │ │ ├── ssh.nix │ │ ├── stratums.nix │ │ ├── system.nix │ │ └── virtualisation.nix │ └── tests │ ├── default.nix │ ├── dhcp.nix │ ├── dns.nix │ ├── filesystems │ ├── default.nix │ └── zfs.nix │ ├── gateway.nix │ └── sandbox.nix └── secrets.nix /.envrc: -------------------------------------------------------------------------------- 1 | use flake 2 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | # Hide diffs for encrypted files 2 | *.age -diff 3 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .terraform/ 2 | .direnv 3 | result 4 | result-* 5 | .gcroots 6 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 3, 29 June 2007 3 | 4 | Copyright (C) 2007 Free Software Foundation, Inc. 5 | Everyone is permitted to copy and distribute verbatim copies 6 | of this license document, but changing it is not allowed. 7 | 8 | Preamble 9 | 10 | The GNU General Public License is a free, copyleft license for 11 | software and other kinds of works. 12 | 13 | The licenses for most software and other practical works are designed 14 | to take away your freedom to share and change the works. By contrast, 15 | the GNU General Public License is intended to guarantee your freedom to 16 | share and change all versions of a program--to make sure it remains free 17 | software for all its users. We, the Free Software Foundation, use the 18 | GNU General Public License for most of our software; it applies also to 19 | any other work released this way by its authors. You can apply it to 20 | your programs, too. 21 | 22 | When we speak of free software, we are referring to freedom, not 23 | price. Our General Public Licenses are designed to make sure that you 24 | have the freedom to distribute copies of free software (and charge for 25 | them if you wish), that you receive source code or can get it if you 26 | want it, that you can change the software or use pieces of it in new 27 | free programs, and that you know you can do these things. 28 | 29 | To protect your rights, we need to prevent others from denying you 30 | these rights or asking you to surrender the rights. Therefore, you have 31 | certain responsibilities if you distribute copies of the software, or if 32 | you modify it: responsibilities to respect the freedom of others. 33 | 34 | For example, if you distribute copies of such a program, whether 35 | gratis or for a fee, you must pass on to the recipients the same 36 | freedoms that you received. You must make sure that they, too, receive 37 | or can get the source code. And you must show them these terms so they 38 | know their rights. 39 | 40 | Developers that use the GNU GPL protect your rights with two steps: 41 | (1) assert copyright on the software, and (2) offer you this License 42 | giving you legal permission to copy, distribute and/or modify it. 43 | 44 | For the developers' and authors' protection, the GPL clearly explains 45 | that there is no warranty for this free software. For both users' and 46 | authors' sake, the GPL requires that modified versions be marked as 47 | changed, so that their problems will not be attributed erroneously to 48 | authors of previous versions. 49 | 50 | Some devices are designed to deny users access to install or run 51 | modified versions of the software inside them, although the manufacturer 52 | can do so. This is fundamentally incompatible with the aim of 53 | protecting users' freedom to change the software. The systematic 54 | pattern of such abuse occurs in the area of products for individuals to 55 | use, which is precisely where it is most unacceptable. Therefore, we 56 | have designed this version of the GPL to prohibit the practice for those 57 | products. If such problems arise substantially in other domains, we 58 | stand ready to extend this provision to those domains in future versions 59 | of the GPL, as needed to protect the freedom of users. 60 | 61 | Finally, every program is threatened constantly by software patents. 62 | States should not allow patents to restrict development and use of 63 | software on general-purpose computers, but in those that do, we wish to 64 | avoid the special danger that patents applied to a free program could 65 | make it effectively proprietary. To prevent this, the GPL assures that 66 | patents cannot be used to render the program non-free. 67 | 68 | The precise terms and conditions for copying, distribution and 69 | modification follow. 70 | 71 | TERMS AND CONDITIONS 72 | 73 | 0. Definitions. 74 | 75 | "This License" refers to version 3 of the GNU General Public License. 76 | 77 | "Copyright" also means copyright-like laws that apply to other kinds of 78 | works, such as semiconductor masks. 79 | 80 | "The Program" refers to any copyrightable work licensed under this 81 | License. Each licensee is addressed as "you". "Licensees" and 82 | "recipients" may be individuals or organizations. 83 | 84 | To "modify" a work means to copy from or adapt all or part of the work 85 | in a fashion requiring copyright permission, other than the making of an 86 | exact copy. The resulting work is called a "modified version" of the 87 | earlier work or a work "based on" the earlier work. 88 | 89 | A "covered work" means either the unmodified Program or a work based 90 | on the Program. 91 | 92 | To "propagate" a work means to do anything with it that, without 93 | permission, would make you directly or secondarily liable for 94 | infringement under applicable copyright law, except executing it on a 95 | computer or modifying a private copy. Propagation includes copying, 96 | distribution (with or without modification), making available to the 97 | public, and in some countries other activities as well. 98 | 99 | To "convey" a work means any kind of propagation that enables other 100 | parties to make or receive copies. Mere interaction with a user through 101 | a computer network, with no transfer of a copy, is not conveying. 102 | 103 | An interactive user interface displays "Appropriate Legal Notices" 104 | to the extent that it includes a convenient and prominently visible 105 | feature that (1) displays an appropriate copyright notice, and (2) 106 | tells the user that there is no warranty for the work (except to the 107 | extent that warranties are provided), that licensees may convey the 108 | work under this License, and how to view a copy of this License. If 109 | the interface presents a list of user commands or options, such as a 110 | menu, a prominent item in the list meets this criterion. 111 | 112 | 1. Source Code. 113 | 114 | The "source code" for a work means the preferred form of the work 115 | for making modifications to it. "Object code" means any non-source 116 | form of a work. 117 | 118 | A "Standard Interface" means an interface that either is an official 119 | standard defined by a recognized standards body, or, in the case of 120 | interfaces specified for a particular programming language, one that 121 | is widely used among developers working in that language. 122 | 123 | The "System Libraries" of an executable work include anything, other 124 | than the work as a whole, that (a) is included in the normal form of 125 | packaging a Major Component, but which is not part of that Major 126 | Component, and (b) serves only to enable use of the work with that 127 | Major Component, or to implement a Standard Interface for which an 128 | implementation is available to the public in source code form. A 129 | "Major Component", in this context, means a major essential component 130 | (kernel, window system, and so on) of the specific operating system 131 | (if any) on which the executable work runs, or a compiler used to 132 | produce the work, or an object code interpreter used to run it. 133 | 134 | The "Corresponding Source" for a work in object code form means all 135 | the source code needed to generate, install, and (for an executable 136 | work) run the object code and to modify the work, including scripts to 137 | control those activities. However, it does not include the work's 138 | System Libraries, or general-purpose tools or generally available free 139 | programs which are used unmodified in performing those activities but 140 | which are not part of the work. For example, Corresponding Source 141 | includes interface definition files associated with source files for 142 | the work, and the source code for shared libraries and dynamically 143 | linked subprograms that the work is specifically designed to require, 144 | such as by intimate data communication or control flow between those 145 | subprograms and other parts of the work. 146 | 147 | The Corresponding Source need not include anything that users 148 | can regenerate automatically from other parts of the Corresponding 149 | Source. 150 | 151 | The Corresponding Source for a work in source code form is that 152 | same work. 153 | 154 | 2. Basic Permissions. 155 | 156 | All rights granted under this License are granted for the term of 157 | copyright on the Program, and are irrevocable provided the stated 158 | conditions are met. This License explicitly affirms your unlimited 159 | permission to run the unmodified Program. The output from running a 160 | covered work is covered by this License only if the output, given its 161 | content, constitutes a covered work. This License acknowledges your 162 | rights of fair use or other equivalent, as provided by copyright law. 163 | 164 | You may make, run and propagate covered works that you do not 165 | convey, without conditions so long as your license otherwise remains 166 | in force. You may convey covered works to others for the sole purpose 167 | of having them make modifications exclusively for you, or provide you 168 | with facilities for running those works, provided that you comply with 169 | the terms of this License in conveying all material for which you do 170 | not control copyright. Those thus making or running the covered works 171 | for you must do so exclusively on your behalf, under your direction 172 | and control, on terms that prohibit them from making any copies of 173 | your copyrighted material outside their relationship with you. 174 | 175 | Conveying under any other circumstances is permitted solely under 176 | the conditions stated below. Sublicensing is not allowed; section 10 177 | makes it unnecessary. 178 | 179 | 3. Protecting Users' Legal Rights From Anti-Circumvention Law. 180 | 181 | No covered work shall be deemed part of an effective technological 182 | measure under any applicable law fulfilling obligations under article 183 | 11 of the WIPO copyright treaty adopted on 20 December 1996, or 184 | similar laws prohibiting or restricting circumvention of such 185 | measures. 186 | 187 | When you convey a covered work, you waive any legal power to forbid 188 | circumvention of technological measures to the extent such circumvention 189 | is effected by exercising rights under this License with respect to 190 | the covered work, and you disclaim any intention to limit operation or 191 | modification of the work as a means of enforcing, against the work's 192 | users, your or third parties' legal rights to forbid circumvention of 193 | technological measures. 194 | 195 | 4. Conveying Verbatim Copies. 196 | 197 | You may convey verbatim copies of the Program's source code as you 198 | receive it, in any medium, provided that you conspicuously and 199 | appropriately publish on each copy an appropriate copyright notice; 200 | keep intact all notices stating that this License and any 201 | non-permissive terms added in accord with section 7 apply to the code; 202 | keep intact all notices of the absence of any warranty; and give all 203 | recipients a copy of this License along with the Program. 204 | 205 | You may charge any price or no price for each copy that you convey, 206 | and you may offer support or warranty protection for a fee. 207 | 208 | 5. Conveying Modified Source Versions. 209 | 210 | You may convey a work based on the Program, or the modifications to 211 | produce it from the Program, in the form of source code under the 212 | terms of section 4, provided that you also meet all of these conditions: 213 | 214 | a) The work must carry prominent notices stating that you modified 215 | it, and giving a relevant date. 216 | 217 | b) The work must carry prominent notices stating that it is 218 | released under this License and any conditions added under section 219 | 7. This requirement modifies the requirement in section 4 to 220 | "keep intact all notices". 221 | 222 | c) You must license the entire work, as a whole, under this 223 | License to anyone who comes into possession of a copy. This 224 | License will therefore apply, along with any applicable section 7 225 | additional terms, to the whole of the work, and all its parts, 226 | regardless of how they are packaged. This License gives no 227 | permission to license the work in any other way, but it does not 228 | invalidate such permission if you have separately received it. 229 | 230 | d) If the work has interactive user interfaces, each must display 231 | Appropriate Legal Notices; however, if the Program has interactive 232 | interfaces that do not display Appropriate Legal Notices, your 233 | work need not make them do so. 234 | 235 | A compilation of a covered work with other separate and independent 236 | works, which are not by their nature extensions of the covered work, 237 | and which are not combined with it such as to form a larger program, 238 | in or on a volume of a storage or distribution medium, is called an 239 | "aggregate" if the compilation and its resulting copyright are not 240 | used to limit the access or legal rights of the compilation's users 241 | beyond what the individual works permit. Inclusion of a covered work 242 | in an aggregate does not cause this License to apply to the other 243 | parts of the aggregate. 244 | 245 | 6. Conveying Non-Source Forms. 246 | 247 | You may convey a covered work in object code form under the terms 248 | of sections 4 and 5, provided that you also convey the 249 | machine-readable Corresponding Source under the terms of this License, 250 | in one of these ways: 251 | 252 | a) Convey the object code in, or embodied in, a physical product 253 | (including a physical distribution medium), accompanied by the 254 | Corresponding Source fixed on a durable physical medium 255 | customarily used for software interchange. 256 | 257 | b) Convey the object code in, or embodied in, a physical product 258 | (including a physical distribution medium), accompanied by a 259 | written offer, valid for at least three years and valid for as 260 | long as you offer spare parts or customer support for that product 261 | model, to give anyone who possesses the object code either (1) a 262 | copy of the Corresponding Source for all the software in the 263 | product that is covered by this License, on a durable physical 264 | medium customarily used for software interchange, for a price no 265 | more than your reasonable cost of physically performing this 266 | conveying of source, or (2) access to copy the 267 | Corresponding Source from a network server at no charge. 268 | 269 | c) Convey individual copies of the object code with a copy of the 270 | written offer to provide the Corresponding Source. This 271 | alternative is allowed only occasionally and noncommercially, and 272 | only if you received the object code with such an offer, in accord 273 | with subsection 6b. 274 | 275 | d) Convey the object code by offering access from a designated 276 | place (gratis or for a charge), and offer equivalent access to the 277 | Corresponding Source in the same way through the same place at no 278 | further charge. You need not require recipients to copy the 279 | Corresponding Source along with the object code. If the place to 280 | copy the object code is a network server, the Corresponding Source 281 | may be on a different server (operated by you or a third party) 282 | that supports equivalent copying facilities, provided you maintain 283 | clear directions next to the object code saying where to find the 284 | Corresponding Source. Regardless of what server hosts the 285 | Corresponding Source, you remain obligated to ensure that it is 286 | available for as long as needed to satisfy these requirements. 287 | 288 | e) Convey the object code using peer-to-peer transmission, provided 289 | you inform other peers where the object code and Corresponding 290 | Source of the work are being offered to the general public at no 291 | charge under subsection 6d. 292 | 293 | A separable portion of the object code, whose source code is excluded 294 | from the Corresponding Source as a System Library, need not be 295 | included in conveying the object code work. 296 | 297 | A "User Product" is either (1) a "consumer product", which means any 298 | tangible personal property which is normally used for personal, family, 299 | or household purposes, or (2) anything designed or sold for incorporation 300 | into a dwelling. In determining whether a product is a consumer product, 301 | doubtful cases shall be resolved in favor of coverage. For a particular 302 | product received by a particular user, "normally used" refers to a 303 | typical or common use of that class of product, regardless of the status 304 | of the particular user or of the way in which the particular user 305 | actually uses, or expects or is expected to use, the product. A product 306 | is a consumer product regardless of whether the product has substantial 307 | commercial, industrial or non-consumer uses, unless such uses represent 308 | the only significant mode of use of the product. 309 | 310 | "Installation Information" for a User Product means any methods, 311 | procedures, authorization keys, or other information required to install 312 | and execute modified versions of a covered work in that User Product from 313 | a modified version of its Corresponding Source. The information must 314 | suffice to ensure that the continued functioning of the modified object 315 | code is in no case prevented or interfered with solely because 316 | modification has been made. 317 | 318 | If you convey an object code work under this section in, or with, or 319 | specifically for use in, a User Product, and the conveying occurs as 320 | part of a transaction in which the right of possession and use of the 321 | User Product is transferred to the recipient in perpetuity or for a 322 | fixed term (regardless of how the transaction is characterized), the 323 | Corresponding Source conveyed under this section must be accompanied 324 | by the Installation Information. But this requirement does not apply 325 | if neither you nor any third party retains the ability to install 326 | modified object code on the User Product (for example, the work has 327 | been installed in ROM). 328 | 329 | The requirement to provide Installation Information does not include a 330 | requirement to continue to provide support service, warranty, or updates 331 | for a work that has been modified or installed by the recipient, or for 332 | the User Product in which it has been modified or installed. Access to a 333 | network may be denied when the modification itself materially and 334 | adversely affects the operation of the network or violates the rules and 335 | protocols for communication across the network. 336 | 337 | Corresponding Source conveyed, and Installation Information provided, 338 | in accord with this section must be in a format that is publicly 339 | documented (and with an implementation available to the public in 340 | source code form), and must require no special password or key for 341 | unpacking, reading or copying. 342 | 343 | 7. Additional Terms. 344 | 345 | "Additional permissions" are terms that supplement the terms of this 346 | License by making exceptions from one or more of its conditions. 347 | Additional permissions that are applicable to the entire Program shall 348 | be treated as though they were included in this License, to the extent 349 | that they are valid under applicable law. If additional permissions 350 | apply only to part of the Program, that part may be used separately 351 | under those permissions, but the entire Program remains governed by 352 | this License without regard to the additional permissions. 353 | 354 | When you convey a copy of a covered work, you may at your option 355 | remove any additional permissions from that copy, or from any part of 356 | it. (Additional permissions may be written to require their own 357 | removal in certain cases when you modify the work.) You may place 358 | additional permissions on material, added by you to a covered work, 359 | for which you have or can give appropriate copyright permission. 360 | 361 | Notwithstanding any other provision of this License, for material you 362 | add to a covered work, you may (if authorized by the copyright holders of 363 | that material) supplement the terms of this License with terms: 364 | 365 | a) Disclaiming warranty or limiting liability differently from the 366 | terms of sections 15 and 16 of this License; or 367 | 368 | b) Requiring preservation of specified reasonable legal notices or 369 | author attributions in that material or in the Appropriate Legal 370 | Notices displayed by works containing it; or 371 | 372 | c) Prohibiting misrepresentation of the origin of that material, or 373 | requiring that modified versions of such material be marked in 374 | reasonable ways as different from the original version; or 375 | 376 | d) Limiting the use for publicity purposes of names of licensors or 377 | authors of the material; or 378 | 379 | e) Declining to grant rights under trademark law for use of some 380 | trade names, trademarks, or service marks; or 381 | 382 | f) Requiring indemnification of licensors and authors of that 383 | material by anyone who conveys the material (or modified versions of 384 | it) with contractual assumptions of liability to the recipient, for 385 | any liability that these contractual assumptions directly impose on 386 | those licensors and authors. 387 | 388 | All other non-permissive additional terms are considered "further 389 | restrictions" within the meaning of section 10. If the Program as you 390 | received it, or any part of it, contains a notice stating that it is 391 | governed by this License along with a term that is a further 392 | restriction, you may remove that term. If a license document contains 393 | a further restriction but permits relicensing or conveying under this 394 | License, you may add to a covered work material governed by the terms 395 | of that license document, provided that the further restriction does 396 | not survive such relicensing or conveying. 397 | 398 | If you add terms to a covered work in accord with this section, you 399 | must place, in the relevant source files, a statement of the 400 | additional terms that apply to those files, or a notice indicating 401 | where to find the applicable terms. 402 | 403 | Additional terms, permissive or non-permissive, may be stated in the 404 | form of a separately written license, or stated as exceptions; 405 | the above requirements apply either way. 406 | 407 | 8. Termination. 408 | 409 | You may not propagate or modify a covered work except as expressly 410 | provided under this License. Any attempt otherwise to propagate or 411 | modify it is void, and will automatically terminate your rights under 412 | this License (including any patent licenses granted under the third 413 | paragraph of section 11). 414 | 415 | However, if you cease all violation of this License, then your 416 | license from a particular copyright holder is reinstated (a) 417 | provisionally, unless and until the copyright holder explicitly and 418 | finally terminates your license, and (b) permanently, if the copyright 419 | holder fails to notify you of the violation by some reasonable means 420 | prior to 60 days after the cessation. 421 | 422 | Moreover, your license from a particular copyright holder is 423 | reinstated permanently if the copyright holder notifies you of the 424 | violation by some reasonable means, this is the first time you have 425 | received notice of violation of this License (for any work) from that 426 | copyright holder, and you cure the violation prior to 30 days after 427 | your receipt of the notice. 428 | 429 | Termination of your rights under this section does not terminate the 430 | licenses of parties who have received copies or rights from you under 431 | this License. If your rights have been terminated and not permanently 432 | reinstated, you do not qualify to receive new licenses for the same 433 | material under section 10. 434 | 435 | 9. Acceptance Not Required for Having Copies. 436 | 437 | You are not required to accept this License in order to receive or 438 | run a copy of the Program. Ancillary propagation of a covered work 439 | occurring solely as a consequence of using peer-to-peer transmission 440 | to receive a copy likewise does not require acceptance. However, 441 | nothing other than this License grants you permission to propagate or 442 | modify any covered work. These actions infringe copyright if you do 443 | not accept this License. Therefore, by modifying or propagating a 444 | covered work, you indicate your acceptance of this License to do so. 445 | 446 | 10. Automatic Licensing of Downstream Recipients. 447 | 448 | Each time you convey a covered work, the recipient automatically 449 | receives a license from the original licensors, to run, modify and 450 | propagate that work, subject to this License. You are not responsible 451 | for enforcing compliance by third parties with this License. 452 | 453 | An "entity transaction" is a transaction transferring control of an 454 | organization, or substantially all assets of one, or subdividing an 455 | organization, or merging organizations. If propagation of a covered 456 | work results from an entity transaction, each party to that 457 | transaction who receives a copy of the work also receives whatever 458 | licenses to the work the party's predecessor in interest had or could 459 | give under the previous paragraph, plus a right to possession of the 460 | Corresponding Source of the work from the predecessor in interest, if 461 | the predecessor has it or can get it with reasonable efforts. 462 | 463 | You may not impose any further restrictions on the exercise of the 464 | rights granted or affirmed under this License. For example, you may 465 | not impose a license fee, royalty, or other charge for exercise of 466 | rights granted under this License, and you may not initiate litigation 467 | (including a cross-claim or counterclaim in a lawsuit) alleging that 468 | any patent claim is infringed by making, using, selling, offering for 469 | sale, or importing the Program or any portion of it. 470 | 471 | 11. Patents. 472 | 473 | A "contributor" is a copyright holder who authorizes use under this 474 | License of the Program or a work on which the Program is based. The 475 | work thus licensed is called the contributor's "contributor version". 476 | 477 | A contributor's "essential patent claims" are all patent claims 478 | owned or controlled by the contributor, whether already acquired or 479 | hereafter acquired, that would be infringed by some manner, permitted 480 | by this License, of making, using, or selling its contributor version, 481 | but do not include claims that would be infringed only as a 482 | consequence of further modification of the contributor version. For 483 | purposes of this definition, "control" includes the right to grant 484 | patent sublicenses in a manner consistent with the requirements of 485 | this License. 486 | 487 | Each contributor grants you a non-exclusive, worldwide, royalty-free 488 | patent license under the contributor's essential patent claims, to 489 | make, use, sell, offer for sale, import and otherwise run, modify and 490 | propagate the contents of its contributor version. 491 | 492 | In the following three paragraphs, a "patent license" is any express 493 | agreement or commitment, however denominated, not to enforce a patent 494 | (such as an express permission to practice a patent or covenant not to 495 | sue for patent infringement). To "grant" such a patent license to a 496 | party means to make such an agreement or commitment not to enforce a 497 | patent against the party. 498 | 499 | If you convey a covered work, knowingly relying on a patent license, 500 | and the Corresponding Source of the work is not available for anyone 501 | to copy, free of charge and under the terms of this License, through a 502 | publicly available network server or other readily accessible means, 503 | then you must either (1) cause the Corresponding Source to be so 504 | available, or (2) arrange to deprive yourself of the benefit of the 505 | patent license for this particular work, or (3) arrange, in a manner 506 | consistent with the requirements of this License, to extend the patent 507 | license to downstream recipients. "Knowingly relying" means you have 508 | actual knowledge that, but for the patent license, your conveying the 509 | covered work in a country, or your recipient's use of the covered work 510 | in a country, would infringe one or more identifiable patents in that 511 | country that you have reason to believe are valid. 512 | 513 | If, pursuant to or in connection with a single transaction or 514 | arrangement, you convey, or propagate by procuring conveyance of, a 515 | covered work, and grant a patent license to some of the parties 516 | receiving the covered work authorizing them to use, propagate, modify 517 | or convey a specific copy of the covered work, then the patent license 518 | you grant is automatically extended to all recipients of the covered 519 | work and works based on it. 520 | 521 | A patent license is "discriminatory" if it does not include within 522 | the scope of its coverage, prohibits the exercise of, or is 523 | conditioned on the non-exercise of one or more of the rights that are 524 | specifically granted under this License. You may not convey a covered 525 | work if you are a party to an arrangement with a third party that is 526 | in the business of distributing software, under which you make payment 527 | to the third party based on the extent of your activity of conveying 528 | the work, and under which the third party grants, to any of the 529 | parties who would receive the covered work from you, a discriminatory 530 | patent license (a) in connection with copies of the covered work 531 | conveyed by you (or copies made from those copies), or (b) primarily 532 | for and in connection with specific products or compilations that 533 | contain the covered work, unless you entered into that arrangement, 534 | or that patent license was granted, prior to 28 March 2007. 535 | 536 | Nothing in this License shall be construed as excluding or limiting 537 | any implied license or other defenses to infringement that may 538 | otherwise be available to you under applicable patent law. 539 | 540 | 12. No Surrender of Others' Freedom. 541 | 542 | If conditions are imposed on you (whether by court order, agreement or 543 | otherwise) that contradict the conditions of this License, they do not 544 | excuse you from the conditions of this License. If you cannot convey a 545 | covered work so as to satisfy simultaneously your obligations under this 546 | License and any other pertinent obligations, then as a consequence you may 547 | not convey it at all. For example, if you agree to terms that obligate you 548 | to collect a royalty for further conveying from those to whom you convey 549 | the Program, the only way you could satisfy both those terms and this 550 | License would be to refrain entirely from conveying the Program. 551 | 552 | 13. Use with the GNU Affero General Public License. 553 | 554 | Notwithstanding any other provision of this License, you have 555 | permission to link or combine any covered work with a work licensed 556 | under version 3 of the GNU Affero General Public License into a single 557 | combined work, and to convey the resulting work. The terms of this 558 | License will continue to apply to the part which is the covered work, 559 | but the special requirements of the GNU Affero General Public License, 560 | section 13, concerning interaction through a network will apply to the 561 | combination as such. 562 | 563 | 14. Revised Versions of this License. 564 | 565 | The Free Software Foundation may publish revised and/or new versions of 566 | the GNU General Public License from time to time. Such new versions will 567 | be similar in spirit to the present version, but may differ in detail to 568 | address new problems or concerns. 569 | 570 | Each version is given a distinguishing version number. If the 571 | Program specifies that a certain numbered version of the GNU General 572 | Public License "or any later version" applies to it, you have the 573 | option of following the terms and conditions either of that numbered 574 | version or of any later version published by the Free Software 575 | Foundation. If the Program does not specify a version number of the 576 | GNU General Public License, you may choose any version ever published 577 | by the Free Software Foundation. 578 | 579 | If the Program specifies that a proxy can decide which future 580 | versions of the GNU General Public License can be used, that proxy's 581 | public statement of acceptance of a version permanently authorizes you 582 | to choose that version for the Program. 583 | 584 | Later license versions may give you additional or different 585 | permissions. However, no additional obligations are imposed on any 586 | author or copyright holder as a result of your choosing to follow a 587 | later version. 588 | 589 | 15. Disclaimer of Warranty. 590 | 591 | THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY 592 | APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT 593 | HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY 594 | OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, 595 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 596 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM 597 | IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF 598 | ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 599 | 600 | 16. Limitation of Liability. 601 | 602 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 603 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS 604 | THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY 605 | GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE 606 | USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF 607 | DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD 608 | PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), 609 | EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF 610 | SUCH DAMAGES. 611 | 612 | 17. Interpretation of Sections 15 and 16. 613 | 614 | If the disclaimer of warranty and limitation of liability provided 615 | above cannot be given local legal effect according to their terms, 616 | reviewing courts shall apply local law that most closely approximates 617 | an absolute waiver of all civil liability in connection with the 618 | Program, unless a warranty or assumption of liability accompanies a 619 | copy of the Program in return for a fee. 620 | 621 | END OF TERMS AND CONDITIONS 622 | 623 | How to Apply These Terms to Your New Programs 624 | 625 | If you develop a new program, and you want it to be of the greatest 626 | possible use to the public, the best way to achieve this is to make it 627 | free software which everyone can redistribute and change under these terms. 628 | 629 | To do so, attach the following notices to the program. It is safest 630 | to attach them to the start of each source file to most effectively 631 | state the exclusion of warranty; and each file should have at least 632 | the "copyright" line and a pointer to where the full notice is found. 633 | 634 | 635 | Copyright (C) 636 | 637 | This program is free software: you can redistribute it and/or modify 638 | it under the terms of the GNU General Public License as published by 639 | the Free Software Foundation, either version 3 of the License, or 640 | (at your option) any later version. 641 | 642 | This program is distributed in the hope that it will be useful, 643 | but WITHOUT ANY WARRANTY; without even the implied warranty of 644 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 645 | GNU General Public License for more details. 646 | 647 | You should have received a copy of the GNU General Public License 648 | along with this program. If not, see . 649 | 650 | Also add information on how to contact you by electronic and paper mail. 651 | 652 | If the program does terminal interaction, make it output a short 653 | notice like this when it starts in an interactive mode: 654 | 655 | Copyright (C) 656 | This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 657 | This is free software, and you are welcome to redistribute it 658 | under certain conditions; type `show c' for details. 659 | 660 | The hypothetical commands `show w' and `show c' should show the appropriate 661 | parts of the General Public License. Of course, your program's commands 662 | might be different; for a GUI interface, you would use an "about box". 663 | 664 | You should also get your employer (if you work as a programmer) or school, 665 | if any, to sign a "copyright disclaimer" for the program, if necessary. 666 | For more information on this, and how to apply and follow the GNU GPL, see 667 | . 668 | 669 | The GNU General Public License does not permit incorporating your program 670 | into proprietary programs. If your program is a subroutine library, you 671 | may consider it more useful to permit linking proprietary applications with 672 | the library. If this is what you want to do, use the GNU Lesser General 673 | Public License instead of this License. But first, please read 674 | . 675 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Home Lab 2 | 3 | A set of [NixOS](https://nixos.org/) modules for building your own on-premise cloud (according to a hobbyist). 4 | 5 | ## Project Status 6 | 7 | :construction: Under Construction :construction: 8 | 9 | This is undergoing a rewrite to incorporate learnings from a few years of working with NixOS. See [ye-olden-days](https://github.com/PsychoLlama/home-lab/tree/ye-olden-days) for a more elaborate, albiet messy example. 10 | 11 | ## Components 12 | 13 | ### Router 14 | 15 | The router module configures a router (nat, dhcp, dns, ...) and manages the network for everything else in the lab. 16 | 17 | ### File Server 18 | 19 | The file storage module manages ZFS pools and datasets. A host profile attaches Syncthing and adds snapshotting. 20 | 21 | ## Project Structure 22 | 23 | - `platforms/nixos/modules/lab`: Unopinionated "library" modules for building a home lab. 24 | - `platforms/nixos/tests`: Virtual machine tests for services in `modules/lab`. 25 | - `platforms/nixos/modules/lab/profiles`: Opinionated configurations. 26 | - `hosts`: Per-host configurations. They are thin wrappers around profiles. 27 | 28 | Tests can be executed by entering a dev shell and running `project test `: 29 | 30 | ```bash 31 | # Example: 32 | project test filesystems.zfs 33 | ``` 34 | 35 | To cripple your machine by running all tests, do: 36 | 37 | ```bash 38 | nix build --verbose '.#tests' 39 | ``` 40 | 41 | ## Inspiration 42 | 43 | The Nix Tradition is reading source code until you figure it out. Here are resources that helped me. 44 | 45 | - [bitte](https://github.com/input-output-hk/bitte) 46 | - [hlissner's dotfiles](https://github.com/hlissner/dotfiles/) 47 | - [ideas for a NixOS router](https://francis.begyn.be/blog/nixos-home-router) 48 | -------------------------------------------------------------------------------- /flake.lock: -------------------------------------------------------------------------------- 1 | { 2 | "nodes": { 3 | "agenix": { 4 | "inputs": { 5 | "darwin": "darwin", 6 | "home-manager": [ 7 | "home-manager" 8 | ], 9 | "nixpkgs": [ 10 | "nixpkgs" 11 | ], 12 | "systems": "systems" 13 | }, 14 | "locked": { 15 | "lastModified": 1745630506, 16 | "narHash": "sha256-bHCFgGeu8XjWlVuaWzi3QONjDW3coZDqSHvnd4l7xus=", 17 | "owner": "ryantm", 18 | "repo": "agenix", 19 | "rev": "96e078c646b711aee04b82ba01aefbff87004ded", 20 | "type": "github" 21 | }, 22 | "original": { 23 | "owner": "ryantm", 24 | "repo": "agenix", 25 | "type": "github" 26 | } 27 | }, 28 | "clapfile": { 29 | "inputs": { 30 | "nixpkgs": "nixpkgs", 31 | "rust-overlay": "rust-overlay" 32 | }, 33 | "locked": { 34 | "lastModified": 1716205723, 35 | "narHash": "sha256-ESjD3MelZ1tcyQRr5PICA3Im7X6UGytAKBfRtPxl7fk=", 36 | "owner": "PsychoLlama", 37 | "repo": "clapfile", 38 | "rev": "687d3cf9b1ed3d8b3b7b3a5100c02302e288c7c8", 39 | "type": "github" 40 | }, 41 | "original": { 42 | "owner": "PsychoLlama", 43 | "repo": "clapfile", 44 | "type": "github" 45 | } 46 | }, 47 | "colmena": { 48 | "inputs": { 49 | "flake-compat": "flake-compat", 50 | "flake-utils": "flake-utils_2", 51 | "nix-github-actions": "nix-github-actions", 52 | "nixpkgs": [ 53 | "nixpkgs-unstable" 54 | ], 55 | "stable": [ 56 | "nixpkgs" 57 | ] 58 | }, 59 | "locked": { 60 | "lastModified": 1739900653, 61 | "narHash": "sha256-hPSLvw6AZQYrZyGI6Uq4XgST7benF/0zcCpugn/P0yM=", 62 | "owner": "zhaofengli", 63 | "repo": "colmena", 64 | "rev": "2370d4336eda2a9ef29fce10fa7076ae011983ab", 65 | "type": "github" 66 | }, 67 | "original": { 68 | "owner": "zhaofengli", 69 | "repo": "colmena", 70 | "type": "github" 71 | } 72 | }, 73 | "darwin": { 74 | "inputs": { 75 | "nixpkgs": [ 76 | "agenix", 77 | "nixpkgs" 78 | ] 79 | }, 80 | "locked": { 81 | "lastModified": 1744478979, 82 | "narHash": "sha256-dyN+teG9G82G+m+PX/aSAagkC+vUv0SgUw3XkPhQodQ=", 83 | "owner": "lnl7", 84 | "repo": "nix-darwin", 85 | "rev": "43975d782b418ebf4969e9ccba82466728c2851b", 86 | "type": "github" 87 | }, 88 | "original": { 89 | "owner": "lnl7", 90 | "ref": "master", 91 | "repo": "nix-darwin", 92 | "type": "github" 93 | } 94 | }, 95 | "flake-compat": { 96 | "flake": false, 97 | "locked": { 98 | "lastModified": 1650374568, 99 | "narHash": "sha256-Z+s0J8/r907g149rllvwhb4pKi8Wam5ij0st8PwAh+E=", 100 | "owner": "edolstra", 101 | "repo": "flake-compat", 102 | "rev": "b4a34015c698c7793d592d66adbab377907a2be8", 103 | "type": "github" 104 | }, 105 | "original": { 106 | "owner": "edolstra", 107 | "repo": "flake-compat", 108 | "type": "github" 109 | } 110 | }, 111 | "flake-utils": { 112 | "inputs": { 113 | "systems": "systems_2" 114 | }, 115 | "locked": { 116 | "lastModified": 1705309234, 117 | "narHash": "sha256-uNRRNRKmJyCRC/8y1RqBkqWBLM034y4qN7EprSdmgyA=", 118 | "owner": "numtide", 119 | "repo": "flake-utils", 120 | "rev": "1ef2e671c3b0c19053962c07dbda38332dcebf26", 121 | "type": "github" 122 | }, 123 | "original": { 124 | "owner": "numtide", 125 | "repo": "flake-utils", 126 | "type": "github" 127 | } 128 | }, 129 | "flake-utils_2": { 130 | "locked": { 131 | "lastModified": 1659877975, 132 | "narHash": "sha256-zllb8aq3YO3h8B/U0/J1WBgAL8EX5yWf5pMj3G0NAmc=", 133 | "owner": "numtide", 134 | "repo": "flake-utils", 135 | "rev": "c0e246b9b83f637f4681389ecabcb2681b4f3af0", 136 | "type": "github" 137 | }, 138 | "original": { 139 | "owner": "numtide", 140 | "repo": "flake-utils", 141 | "type": "github" 142 | } 143 | }, 144 | "home-manager": { 145 | "inputs": { 146 | "nixpkgs": [ 147 | "nixpkgs" 148 | ] 149 | }, 150 | "locked": { 151 | "lastModified": 1746171682, 152 | "narHash": "sha256-EyXUNSa+H+YvGVuQJP1nZskXAowxKYp79RNUsNdQTj4=", 153 | "owner": "nix-community", 154 | "repo": "home-manager", 155 | "rev": "50eee705bbdbac942074a8c120e8194185633675", 156 | "type": "github" 157 | }, 158 | "original": { 159 | "owner": "nix-community", 160 | "ref": "release-24.11", 161 | "repo": "home-manager", 162 | "type": "github" 163 | } 164 | }, 165 | "nix-github-actions": { 166 | "inputs": { 167 | "nixpkgs": [ 168 | "colmena", 169 | "nixpkgs" 170 | ] 171 | }, 172 | "locked": { 173 | "lastModified": 1729742964, 174 | "narHash": "sha256-B4mzTcQ0FZHdpeWcpDYPERtyjJd/NIuaQ9+BV1h+MpA=", 175 | "owner": "nix-community", 176 | "repo": "nix-github-actions", 177 | "rev": "e04df33f62cdcf93d73e9a04142464753a16db67", 178 | "type": "github" 179 | }, 180 | "original": { 181 | "owner": "nix-community", 182 | "repo": "nix-github-actions", 183 | "type": "github" 184 | } 185 | }, 186 | "nixos-hardware": { 187 | "locked": { 188 | "lastModified": 1746341346, 189 | "narHash": "sha256-WjupK5Xpc+viJlJWiyPHp/dF4aJItp1BPuFsEdv2/fI=", 190 | "owner": "NixOS", 191 | "repo": "nixos-hardware", 192 | "rev": "0833dc8bbc4ffa9cf9b0cbfccf1c5ec8632fc66e", 193 | "type": "github" 194 | }, 195 | "original": { 196 | "owner": "NixOS", 197 | "repo": "nixos-hardware", 198 | "type": "github" 199 | } 200 | }, 201 | "nixpkgs": { 202 | "locked": { 203 | "lastModified": 1709613862, 204 | "narHash": "sha256-mH+c2gFEzEe49lhUWJ0ieIaMaJ1W85E6G1xLm8ege90=", 205 | "path": "/nix/store/y3wcwikr0fl9jysgn2fmg9kwf7sp3s6d-source", 206 | "rev": "311a4be96d940a0c673e88bd5bc83ea4f005cc02", 207 | "type": "path" 208 | }, 209 | "original": { 210 | "id": "nixpkgs", 211 | "type": "indirect" 212 | } 213 | }, 214 | "nixpkgs-unstable": { 215 | "locked": { 216 | "lastModified": 1746300365, 217 | "narHash": "sha256-thYTdWqCRipwPRxWiTiH1vusLuAy0okjOyzRx4hLWh4=", 218 | "owner": "NixOS", 219 | "repo": "nixpkgs", 220 | "rev": "f21e4546e3ede7ae34d12a84602a22246b31f7e0", 221 | "type": "github" 222 | }, 223 | "original": { 224 | "owner": "NixOS", 225 | "ref": "nixpkgs-unstable", 226 | "repo": "nixpkgs", 227 | "type": "github" 228 | } 229 | }, 230 | "nixpkgs_2": { 231 | "locked": { 232 | "lastModified": 1746183838, 233 | "narHash": "sha256-kwaaguGkAqTZ1oK0yXeQ3ayYjs8u/W7eEfrFpFfIDFA=", 234 | "owner": "NixOS", 235 | "repo": "nixpkgs", 236 | "rev": "bf3287dac860542719fe7554e21e686108716879", 237 | "type": "github" 238 | }, 239 | "original": { 240 | "owner": "NixOS", 241 | "ref": "nixos-24.11", 242 | "repo": "nixpkgs", 243 | "type": "github" 244 | } 245 | }, 246 | "root": { 247 | "inputs": { 248 | "agenix": "agenix", 249 | "clapfile": "clapfile", 250 | "colmena": "colmena", 251 | "home-manager": "home-manager", 252 | "nixos-hardware": "nixos-hardware", 253 | "nixpkgs": "nixpkgs_2", 254 | "nixpkgs-unstable": "nixpkgs-unstable" 255 | } 256 | }, 257 | "rust-overlay": { 258 | "inputs": { 259 | "flake-utils": "flake-utils", 260 | "nixpkgs": [ 261 | "clapfile", 262 | "nixpkgs" 263 | ] 264 | }, 265 | "locked": { 266 | "lastModified": 1710641527, 267 | "narHash": "sha256-R9JZEevtSyg7++LEryYJRrfyEe45azJxmu2k9VezEW0=", 268 | "owner": "oxalica", 269 | "repo": "rust-overlay", 270 | "rev": "50db54295d3922a3b7a40d580b84d75150b36c34", 271 | "type": "github" 272 | }, 273 | "original": { 274 | "owner": "oxalica", 275 | "repo": "rust-overlay", 276 | "type": "github" 277 | } 278 | }, 279 | "systems": { 280 | "locked": { 281 | "lastModified": 1681028828, 282 | "narHash": "sha256-Vy1rq5AaRuLzOxct8nz4T6wlgyUR7zLU309k9mBC768=", 283 | "owner": "nix-systems", 284 | "repo": "default", 285 | "rev": "da67096a3b9bf56a91d16901293e51ba5b49a27e", 286 | "type": "github" 287 | }, 288 | "original": { 289 | "owner": "nix-systems", 290 | "repo": "default", 291 | "type": "github" 292 | } 293 | }, 294 | "systems_2": { 295 | "locked": { 296 | "lastModified": 1681028828, 297 | "narHash": "sha256-Vy1rq5AaRuLzOxct8nz4T6wlgyUR7zLU309k9mBC768=", 298 | "owner": "nix-systems", 299 | "repo": "default", 300 | "rev": "da67096a3b9bf56a91d16901293e51ba5b49a27e", 301 | "type": "github" 302 | }, 303 | "original": { 304 | "owner": "nix-systems", 305 | "repo": "default", 306 | "type": "github" 307 | } 308 | } 309 | }, 310 | "root": "root", 311 | "version": 7 312 | } 313 | -------------------------------------------------------------------------------- /flake.nix: -------------------------------------------------------------------------------- 1 | { 2 | description = "Hobbyist home lab"; 3 | 4 | inputs = { 5 | nixpkgs-unstable.url = "github:NixOS/nixpkgs/nixpkgs-unstable"; 6 | nixpkgs.url = "github:NixOS/nixpkgs/nixos-24.11"; 7 | nixos-hardware.url = "github:NixOS/nixos-hardware"; 8 | clapfile.url = "github:PsychoLlama/clapfile"; 9 | 10 | home-manager = { 11 | url = "github:nix-community/home-manager/release-24.11"; 12 | inputs.nixpkgs.follows = "nixpkgs"; 13 | }; 14 | 15 | colmena = { 16 | url = "github:zhaofengli/colmena"; 17 | inputs = { 18 | nixpkgs.follows = "nixpkgs-unstable"; 19 | stable.follows = "nixpkgs"; 20 | }; 21 | }; 22 | 23 | agenix = { 24 | url = "github:ryantm/agenix"; 25 | inputs = { 26 | home-manager.follows = "home-manager"; 27 | nixpkgs.follows = "nixpkgs"; 28 | }; 29 | }; 30 | }; 31 | 32 | outputs = 33 | { 34 | self, 35 | nixpkgs-unstable, 36 | nixpkgs, 37 | nixos-hardware, 38 | colmena, 39 | clapfile, 40 | agenix, 41 | ... 42 | }@flake-inputs: 43 | 44 | let 45 | inherit (nixpkgs-unstable) lib; 46 | inherit (import ./lib flake-inputs) defineHost deviceProfiles makeImage; 47 | 48 | domain = "selfhosted.city"; 49 | datacenter = "nova"; 50 | 51 | # A subset of Hydra's standard architectures. 52 | standardSystems = [ 53 | "x86_64-linux" 54 | "aarch64-linux" 55 | ]; 56 | 57 | # Necessary evils with non-free licenses. 58 | evilPackages = [ ]; 59 | 60 | # Load nixpkgs with home-lab overrides. 61 | loadPkgs = 62 | { system }: 63 | import nixpkgs { 64 | inherit system; 65 | 66 | config = { 67 | allowUnfreePredicate = pkg: lib.elem (lib.getName pkg) evilPackages; 68 | }; 69 | 70 | overlays = [ 71 | self.overlays.unstable-packages 72 | clapfile.overlays.programs 73 | ]; 74 | }; 75 | 76 | # Attrs { system -> pkgs } 77 | packageUniverse = lib.genAttrs standardSystems (system: loadPkgs { inherit system; }); 78 | 79 | eachSystem = lib.flip lib.mapAttrs packageUniverse; 80 | 81 | # Each record maps to `config.lab.host`. 82 | hosts = with deviceProfiles; { 83 | rpi3-001 = { 84 | module = ./hosts/rpi3-001.nix; 85 | profile = raspberry-pi-3; 86 | system = "aarch64-linux"; 87 | ip4 = "10.0.0.203"; 88 | interface = "enu1u1"; 89 | publicKeys = [ "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIN2VZGgphnMAD5tLG+IHBlBWdlUPNfvYEMDK8OQCrG/A" ]; 90 | }; 91 | rpi3-002 = { 92 | module = ./hosts/rpi3-002.nix; 93 | profile = raspberry-pi-3; 94 | system = "aarch64-linux"; 95 | ip4 = "10.0.0.202"; 96 | interface = "enu1u1"; 97 | publicKeys = [ "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIKrGfslz9RlB2EzrTL3SfO/NZB5fPiVXWkK+aQRZrlel" ]; 98 | }; 99 | rpi4-001 = { 100 | module = ./hosts/rpi4-001.nix; 101 | profile = raspberry-pi-4; 102 | system = "aarch64-linux"; 103 | ip4 = "10.0.0.1"; # Router 104 | interface = null; # No "primary" interface. 105 | publicKeys = [ "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIAyb4vh9xDEEV+30G0UPMTSdtVq3Tyfgl9I9VRwf226v" ]; 106 | }; 107 | rpi4-002 = { 108 | module = ./hosts/rpi4-002.nix; 109 | profile = raspberry-pi-4; 110 | system = "aarch64-linux"; 111 | ip4 = "10.0.0.208"; 112 | interface = "end0"; 113 | builder.enable = true; 114 | publicKeys = [ "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIJLMZ6+HaPahE4gGIAWW/uGIl/y40p/rSfIhb5t4G+g9" ]; 115 | }; 116 | rpi4-003 = { 117 | module = ./hosts/rpi4-003.nix; 118 | profile = raspberry-pi-4; 119 | system = "aarch64-linux"; 120 | ip4 = "10.0.0.204"; 121 | interface = "end0"; 122 | builder.enable = true; 123 | publicKeys = [ "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIFsNbo3bbm0G11GAbRwnr944AitRyqoQMN4LG7rMsvpK" ]; 124 | }; 125 | }; 126 | 127 | hive = colmena.lib.makeHive self.colmena; 128 | in 129 | { 130 | nixosModules = { 131 | nixos-platform = ./platforms/nixos/modules; 132 | home-manager-platform = ./platforms/home-manager/modules; 133 | }; 134 | 135 | overlays = { 136 | # Add `pkgs.unstable` to the package set. 137 | unstable-packages = final: prev: { 138 | unstable = import nixpkgs-unstable { inherit (prev) system config; }; 139 | }; 140 | }; 141 | 142 | # Workaround for unlocked inputs in pure evaluations using newer 143 | # versions of Nix. Supports Colmena's `--experimental-flake-eval` flag. 144 | # See: https://github.com/zhaofengli/colmena/issues/202 145 | colmenaHive = hive; 146 | 147 | colmena = (lib.mapAttrs defineHost hosts) // rec { 148 | defaults.lab = { 149 | inherit datacenter domain; 150 | 151 | networks = { 152 | datacenter.ipv4 = { 153 | cidr = "10.0.0.1/24"; 154 | dhcp.pools = [ 155 | { 156 | start = "10.0.0.10"; 157 | end = "10.0.0.200"; 158 | } 159 | ]; 160 | }; 161 | 162 | home.ipv4 = { 163 | cidr = "10.0.1.1/24"; 164 | dhcp.pools = [ 165 | { 166 | start = "10.0.1.10"; 167 | end = "10.0.1.250"; 168 | } 169 | ]; 170 | }; 171 | 172 | iot.ipv4 = { 173 | cidr = "10.0.2.1/24"; 174 | dhcp.pools = [ 175 | { 176 | start = "10.0.2.10"; 177 | end = "10.0.2.250"; 178 | } 179 | ]; 180 | }; 181 | 182 | work.ipv4 = { 183 | cidr = "10.0.3.1/24"; 184 | dhcp.pools = [ 185 | { 186 | start = "10.0.3.10"; 187 | end = "10.0.3.250"; 188 | } 189 | ]; 190 | }; 191 | 192 | guest.ipv4 = { 193 | cidr = "10.0.4.1/24"; 194 | dhcp.pools = [ 195 | { 196 | start = "10.0.4.10"; 197 | end = "10.0.4.250"; 198 | } 199 | ]; 200 | }; 201 | }; 202 | }; 203 | 204 | meta = { 205 | description = defaults.lab.domain; 206 | 207 | nixpkgs = loadPkgs { 208 | # This value is required, but I want host to specify it instead. 209 | # By selecting an intentionally wrong value they are forced to 210 | # override it; Bad things will happen if they do not. 211 | system = "riscv64-linux"; 212 | }; 213 | 214 | # Match each host with the packages for its architecture. 215 | nodeNixpkgs = lib.mapAttrs (_: host: packageUniverse.${host.system}) hosts; 216 | }; 217 | }; 218 | 219 | devShells = eachSystem ( 220 | system: pkgs: 221 | let 222 | baseShellEnvironment = pkgs.mkShell { 223 | packages = [ 224 | pkgs.nixVersions.latest 225 | colmena.packages.${system}.colmena 226 | agenix.packages.${system}.default 227 | 228 | (pkgs.clapfile.command { 229 | command = { 230 | name = "project"; 231 | about = "Project task runner"; 232 | subcommands = { 233 | bootstrap = { 234 | about = "Build a bootable image for a specific host."; 235 | run = pkgs.writers.writeBash "bootstrap" '' 236 | set -eux 237 | nix build ".#packages.$arch.$host-image" 238 | readlink -f result 239 | ''; 240 | 241 | args = [ 242 | { 243 | id = "host"; 244 | required = true; 245 | } 246 | { 247 | id = "arch"; 248 | long = "arch"; 249 | value_name = "system"; 250 | default_value = "aarch64-linux"; 251 | } 252 | ]; 253 | }; 254 | 255 | sandbox = { 256 | about = "Enter a VM sandbox for experimentation."; 257 | run = pkgs.writers.writeBash "sandbox" '' 258 | set -eux 259 | nix run ".#tests.sandbox.driver" 260 | ''; 261 | }; 262 | 263 | test = { 264 | about = "Run one of the tests under `nixos/tests`."; 265 | run = pkgs.writers.writeBash "test" '' 266 | set -eux 267 | nix run ".#tests.$expr.driver" 268 | ''; 269 | 270 | args = [ 271 | { 272 | id = "expr"; 273 | value_name = "test-path"; 274 | help = "dot.separated test path under `outputs.tests`"; 275 | required = true; 276 | } 277 | ]; 278 | }; 279 | 280 | vpn = { 281 | about = "Manage the VPN."; 282 | subcommands.register = { 283 | about = "Register a node on the VPN."; 284 | args = [ 285 | { 286 | id = "host"; 287 | about = "Host to initialize."; 288 | required = true; 289 | } 290 | { 291 | id = "server_url"; 292 | about = "URL of the VPN server."; 293 | short = "s"; 294 | long = "server-url"; 295 | default_value = "http://rpi4-003.host.${datacenter}.${domain}:8080"; 296 | } 297 | ]; 298 | 299 | # TODO: Use Colmena's deploy key commands instead and 300 | # defer the oneshot setup by the key service. 301 | run = pkgs.unstable.writers.writeNu "bootstrap-vpn-client.nu" '' 302 | use std/log 303 | 304 | let server_host = $env.server_url | url parse | get host 305 | let response = ssh $server_host headscale preauthkey create --user dc-${datacenter} --output json | from json 306 | log info $"Auth key created id=($response.id)" 307 | 308 | ssh $env.host tailscale up --login-server $env.server_url --auth-key $response.key 309 | log info "VPN client ready" 310 | ''; 311 | }; 312 | }; 313 | }; 314 | }; 315 | }) 316 | ]; 317 | 318 | # NOTE: Configuring remote builds through the client assumes you 319 | # are a trusted Nix user. Without permission, you'll see errors 320 | # where it refuses to compile a foreign architecture. 321 | NIX_CONFIG = '' 322 | experimental-features = nix-command flakes 323 | builders-use-substitutes = true 324 | builders = @${pkgs.writeText "nix-remote-builders" '' 325 | ${lib.pipe hive.nodes [ 326 | (lib.mapAttrs (_: node: node.config.lab.host)) 327 | (lib.filterAttrs (_: host: host.builder.enable)) 328 | (lib.mapAttrsToList (_: host: host.builder.conf)) 329 | (lib.concatStringsSep "\n") 330 | ]} 331 | ''} 332 | ''; 333 | }; 334 | 335 | # Some modules require special tools or languages for development. 336 | # The pattern is to take the base development shell and extend it. 337 | devShellSpecializations = lib.mergeAttrsList ( 338 | map ( 339 | relativePath: 340 | 341 | let 342 | absolutePath = ./. + "/${relativePath}"; 343 | customizeShell = import absolutePath { inherit pkgs; }; 344 | shell = baseShellEnvironment.overrideAttrs customizeShell; 345 | dirname = lib.pipe relativePath [ 346 | (lib.splitString "/") 347 | (lib.reverseList) 348 | (lib.drop 1) 349 | (lib.reverseList) 350 | (lib.concatStringsSep "/") 351 | (dir: dir + "/") 352 | ]; 353 | in 354 | 355 | { 356 | ${dirname} = shell; 357 | } 358 | ) [ "nixos/modules/lab/filesystems/zfs/develop.nix" ] 359 | ); 360 | in 361 | 362 | # Each shell is indexed by its relative project path. This avoids 363 | # conflicts and can be derived using `git rev-parse --show-prefix`. 364 | devShellSpecializations // { default = baseShellEnvironment; } 365 | ); 366 | 367 | packages = 368 | let 369 | # Create a bootable disk image for each machine. 370 | hostImages = lib.foldlAttrs ( 371 | packages: hostName: node: 372 | lib.recursiveUpdate packages { 373 | ${node.pkgs.system}."${hostName}-image" = makeImage { 374 | inherit nixpkgs; 375 | nixosSystem = node; 376 | }; 377 | } 378 | ) { } hive.nodes; 379 | 380 | # Create a pseudo-package `tests` that holds all `nixosTest` drvs 381 | # underneath. This is useful to escape the flat namespace constraint 382 | # of `flake.packages` while remaining easily scriptable. 383 | testScripts = eachSystem ( 384 | system: pkgs: { 385 | docs = pkgs.callPackage ./platforms/nixos/doc { 386 | inherit (flake-inputs) colmena home-manager clapfile; 387 | revision = self.rev or "latest"; 388 | }; 389 | 390 | # Building this package will run all tests. This is probably not 391 | # what you want. Instead, build individual tests by path. 392 | tests = pkgs.stdenvNoCC.mkDerivation rec { 393 | name = "tests"; 394 | phases = [ "installPhase" ]; 395 | 396 | buildInputs = lib.collect (value: value ? __test) passthru; 397 | installPhase = '' 398 | touch $out 399 | ''; 400 | 401 | # All tests are exposed as attributes on this derivation. You can 402 | # build them by path: 403 | # ``` 404 | # nix build .#tests.. 405 | # ``` 406 | passthru = pkgs.callPackage ./platforms/nixos/tests { 407 | inherit (flake-inputs) 408 | colmena 409 | clapfile 410 | home-manager 411 | agenix 412 | ; 413 | }; 414 | }; 415 | } 416 | ); 417 | in 418 | lib.recursiveUpdate hostImages testScripts; 419 | }; 420 | } 421 | -------------------------------------------------------------------------------- /hosts/rpi3-001.nix: -------------------------------------------------------------------------------- 1 | { 2 | lab.profiles = { 3 | vpn.client.enable = true; 4 | }; 5 | 6 | home-manager.users.root.home.stateVersion = "23.11"; 7 | system.stateVersion = "23.05"; 8 | } 9 | -------------------------------------------------------------------------------- /hosts/rpi3-002.nix: -------------------------------------------------------------------------------- 1 | { 2 | lab.profiles = { 3 | vpn.client.enable = true; 4 | 5 | # File server is temporarily disabled. 2/3 drives corrupted. 6 | # I'm a terrible sysadmin. 7 | file-server.enable = false; 8 | }; 9 | 10 | networking.hostId = "e3cda066"; # Required by ZFS 11 | 12 | home-manager.users.root.home.stateVersion = "23.11"; 13 | system.stateVersion = "23.05"; 14 | } 15 | -------------------------------------------------------------------------------- /hosts/rpi4-001.nix: -------------------------------------------------------------------------------- 1 | { config, ... }: 2 | 3 | let 4 | inherit (config.lab.services.gateway.networks) datacenter home; 5 | inherit (config.lab.services.gateway) wan; 6 | in 7 | { 8 | lab.profiles = { 9 | router.enable = true; 10 | vpn.client.enable = true; 11 | }; 12 | 13 | # Assign sensible names to the network interfaces. Anything with vlans needs 14 | # a hardware-related filter to avoid conflicts with virtual devices. 15 | services.udev.extraRules = '' 16 | SUBSYSTEM=="net", ACTION=="add", ATTR{address}=="b0:a7:b9:2c:a9:b5", NAME="${home.interface}", ENV{ID_BUS}=="usb" 17 | SUBSYSTEM=="net", ACTION=="add", ATTR{address}=="dc:a6:32:e1:42:81", NAME="${datacenter.interface}" 18 | SUBSYSTEM=="net", ACTION=="add", ATTR{address}=="60:a4:b7:59:07:f2", NAME="${wan.interface}" 19 | ''; 20 | 21 | home-manager.users.root.home.stateVersion = "23.11"; 22 | system.stateVersion = "21.05"; 23 | } 24 | -------------------------------------------------------------------------------- /hosts/rpi4-002.nix: -------------------------------------------------------------------------------- 1 | { 2 | lab.profiles = { 3 | observability.enable = true; 4 | vpn.client.enable = true; 5 | }; 6 | 7 | home-manager.users.root.home.stateVersion = "23.11"; 8 | system.stateVersion = "21.11"; 9 | } 10 | -------------------------------------------------------------------------------- /hosts/rpi4-003.nix: -------------------------------------------------------------------------------- 1 | { 2 | lab.profiles = { 3 | vpn.server.enable = true; 4 | vpn.client.enable = true; 5 | }; 6 | 7 | home-manager.users.root.home.stateVersion = "23.11"; 8 | system.stateVersion = "21.05"; 9 | } 10 | -------------------------------------------------------------------------------- /lib/default.nix: -------------------------------------------------------------------------------- 1 | flake-inputs: 2 | 3 | { 4 | defineHost = import ./define-host.nix flake-inputs; 5 | deviceProfiles = import ./device-profiles.nix flake-inputs; 6 | makeImage = import ./make-image.nix; 7 | } 8 | -------------------------------------------------------------------------------- /lib/define-host.nix: -------------------------------------------------------------------------------- 1 | # Creates a top-level NixOS config, applied to all machines in the home lab. 2 | # This is responsible for setting the baseline configuration. 3 | inputs: hostName: host: 4 | 5 | { 6 | config, 7 | lib, 8 | pkgs, 9 | ... 10 | }: 11 | 12 | let 13 | inherit (config.lab) domain datacenter; 14 | home = config.home-manager.users.root; 15 | 16 | # Chunk a list into sublists of a specified size. 17 | # 18 | # chunkBy 2 [ 1 2 3 4 ] -> [ [ 1 2 ] [ 3 4 ] ] 19 | chunkBy = 20 | size: list: 21 | if list == [ ] then 22 | [ ] 23 | else 24 | [ 25 | (lib.take size list) 26 | ] 27 | ++ chunkBy size (lib.drop size list); 28 | 29 | # Proxy all network traffic through a macvlan interface. This allows the 30 | # host to communicate with containers using macvlans and vice versa. 31 | # 32 | # WARN: Shortly after enabling macvlan interfaces, a test deploy broke 33 | # networking since the macvlan was not available by the time dhcpcd started. 34 | # There seems to be a race. 35 | macvlan-proxy = { 36 | config.networking = lib.mkIf (config.lab.host.interface != null) { 37 | useDHCP = false; 38 | 39 | interfaces.mv-primary.useDHCP = true; 40 | macvlans.mv-primary = { 41 | mode = "bridge"; 42 | interface = config.lab.host.interface; 43 | }; 44 | 45 | # Provide a stable MAC address to preserve the DHCP lease. 46 | interfaces.mv-primary.macAddress = lib.pipe config.networking.hostName [ 47 | # Use the hostname hash to derive the MAC address. 48 | (builtins.hashString "md5") 49 | 50 | # [ "" "f" "0" .. "1" "e" "" ] 51 | (lib.splitString "") 52 | 53 | # [ "f" "0" .. "1" "e" ] 54 | (lib.filter (char: char != "")) 55 | 56 | # Take the first 12 characters (for a 6-byte MAC address). 57 | (lib.take 12) 58 | 59 | # [ 15 0 .. 1 14 ] 60 | (map lib.fromHexString) 61 | 62 | # Make sure the last bits of the first byte are `0b10` to indicate 63 | # a locally-administered unicast MAC address. 64 | (lib.imap0 (index: value: if index == 1 then lib.bitAnd (-2) (lib.bitOr value 2) else value)) 65 | 66 | # [ "F" "2" .. "1" "E" ] 67 | (map lib.toHexString) 68 | 69 | # [ [ 15 2 ] .. [ 1 14 ] ] 70 | (chunkBy 2) 71 | 72 | # [ "F2" .. "1E" ] 73 | (lib.map (lib.concatStringsSep "")) 74 | 75 | # "F2:<...>:1E" 76 | (lib.concatStringsSep ":") 77 | ]; 78 | }; 79 | }; 80 | 81 | in 82 | 83 | { 84 | imports = [ 85 | inputs.home-manager.nixosModules.home-manager 86 | inputs.clapfile.nixosModules.nixos 87 | inputs.self.nixosModules.nixos-platform 88 | inputs.agenix.nixosModules.default 89 | 90 | host.profile 91 | host.module 92 | 93 | macvlan-proxy 94 | ]; 95 | 96 | deployment.targetHost = config.networking.fqdn; 97 | environment.sessionVariables.DATACENTER = datacenter; 98 | 99 | networking = { 100 | hostName = lib.mkDefault hostName; 101 | domain = "host.${datacenter}.${domain}"; 102 | }; 103 | 104 | nix = { 105 | # Run garbage collection on a schedule. 106 | gc.automatic = true; 107 | 108 | # Use hard links to save disk space. 109 | optimise.automatic = true; 110 | 111 | # Enable Flake support. 112 | settings.experimental-features = [ 113 | "nix-command" 114 | "flakes" 115 | ]; 116 | }; 117 | 118 | services.openssh = { 119 | enable = true; 120 | settings.PasswordAuthentication = false; 121 | }; 122 | 123 | home-manager = { 124 | useGlobalPkgs = lib.mkDefault true; 125 | useUserPackages = lib.mkDefault true; 126 | sharedModules = [ 127 | inputs.self.nixosModules.home-manager-platform 128 | 129 | { 130 | # Manage the system shell by default. 131 | lab.profiles.fancy-shell.enable = lib.mkDefault true; 132 | } 133 | ]; 134 | }; 135 | 136 | lab = { 137 | inherit host; 138 | 139 | ssh = { 140 | enable = lib.mkDefault true; 141 | authorizedKeys = [ 142 | ./keys/deploy.pub 143 | ./keys/admin.pub 144 | ]; 145 | }; 146 | 147 | virtualisation.sharedModules = [ 148 | # TODO: Add all platforms to the container namespace. 149 | ]; 150 | }; 151 | 152 | users.defaultUserShell = home.programs.nushell.package; 153 | } 154 | -------------------------------------------------------------------------------- /lib/device-profiles.nix: -------------------------------------------------------------------------------- 1 | # Flake inputs given manually, not by the NixOS module system. 2 | { nixos-hardware, nixpkgs, ... }: 3 | 4 | { 5 | raspberry-pi-3 = { 6 | imports = [ nixpkgs.nixosModules.notDetected ]; 7 | deployment.tags = [ "rpi3" ]; 8 | 9 | boot.loader = { 10 | # Grub is not compatible here. Prefer the Extlinux boot loader. 11 | grub.enable = false; 12 | 13 | # The RPI3 module for U-Boot works just as well, but Extlinux should be 14 | # lighter. I don't need anything fancy. 15 | generic-extlinux-compatible.enable = true; 16 | }; 17 | 18 | fileSystems."/" = { 19 | device = "/dev/disk/by-label/NIXOS_SD"; 20 | fsType = "ext4"; 21 | options = [ "noatime" ]; 22 | }; 23 | 24 | # The Pi 3 has severely limited RAM. 25 | swapDevices = [ 26 | { 27 | device = "/var/swapfile"; 28 | size = 1024; 29 | } 30 | ]; 31 | }; 32 | 33 | raspberry-pi-4 = { 34 | imports = [ 35 | nixos-hardware.nixosModules.raspberry-pi-4 36 | nixpkgs.nixosModules.notDetected 37 | ]; 38 | 39 | deployment.tags = [ "rpi4" ]; 40 | 41 | fileSystems."/" = { 42 | device = "/dev/disk/by-label/NIXOS_SD"; 43 | fsType = "ext4"; 44 | options = [ "noatime" ]; 45 | }; 46 | 47 | # Enable GPU acceleration. 48 | hardware.raspberry-pi."4".fkms-3d.enable = true; 49 | 50 | # Enable audio. 51 | hardware.pulseaudio.enable = true; 52 | 53 | # Necessary for building boot images and running NixOS tests. 54 | lab.host.builder.supportedFeatures = [ 55 | "benchmark" 56 | "big-parallel" 57 | "kvm" 58 | "nixos-test" 59 | ]; 60 | }; 61 | } 62 | -------------------------------------------------------------------------------- /lib/keys/admin.pub: -------------------------------------------------------------------------------- 1 | ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIHAMADENOb8Pe0kysfLc6BxK2VUiPMt57IOaDYa7J/M5 JesseTheGibson@gmail.com -------------------------------------------------------------------------------- /lib/keys/deploy.pub: -------------------------------------------------------------------------------- 1 | ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIMkXjJPcjT6vwl1gB9ok8oQ6OiCMzqmeSyOulFyVg+tp Deploy key 2 | -------------------------------------------------------------------------------- /lib/make-image.nix: -------------------------------------------------------------------------------- 1 | { nixosSystem, nixpkgs }: 2 | 3 | # Infinite thanks to taylor1791 for source diving to figure this out. 4 | # See: https://nixos.org/manual/nixpkgs/unstable/#sec-make-disk-image 5 | import "${nixpkgs}/nixos/lib/make-disk-image.nix" { 6 | inherit (nixosSystem) config pkgs; 7 | 8 | bootSize = "1G"; 9 | format = "raw"; 10 | lib = nixpkgs.lib; 11 | partitionTableType = "hybrid"; 12 | } 13 | -------------------------------------------------------------------------------- /platforms/home-manager/modules/default.nix: -------------------------------------------------------------------------------- 1 | { imports = [ ./lab ]; } 2 | -------------------------------------------------------------------------------- /platforms/home-manager/modules/lab/default.nix: -------------------------------------------------------------------------------- 1 | { 2 | imports = [ 3 | ./presets 4 | ./profiles 5 | ]; 6 | } 7 | -------------------------------------------------------------------------------- /platforms/home-manager/modules/lab/presets/default.nix: -------------------------------------------------------------------------------- 1 | { 2 | imports = [ ./programs ]; 3 | } 4 | -------------------------------------------------------------------------------- /platforms/home-manager/modules/lab/presets/programs/default.nix: -------------------------------------------------------------------------------- 1 | { 2 | imports = [ 3 | ./nushell 4 | ./starship.nix 5 | ]; 6 | } 7 | -------------------------------------------------------------------------------- /platforms/home-manager/modules/lab/presets/programs/nushell/config.nu: -------------------------------------------------------------------------------- 1 | $env.config = { 2 | edit_mode: vi 3 | show_banner: false 4 | footer_mode: 20 5 | history: { 6 | file_format: sqlite 7 | isolation: true 8 | } 9 | table: { 10 | mode: rounded 11 | header_on_separator: true 12 | } 13 | cursor_shape: { 14 | vi_normal: block 15 | vi_insert: line 16 | } 17 | completions: { 18 | case_sensitive: false 19 | partial: false 20 | quick: true 21 | external: { 22 | enable: true 23 | } 24 | } 25 | 26 | # Pulled from `config nu --default` 27 | shell_integration: { 28 | osc2: true 29 | osc7: true 30 | osc8: true 31 | osc9_9: false 32 | osc133: true 33 | reset_application_mode: true 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /platforms/home-manager/modules/lab/presets/programs/nushell/default.nix: -------------------------------------------------------------------------------- 1 | { 2 | lib, 3 | config, 4 | pkgs, 5 | ... 6 | }: 7 | 8 | let 9 | cfg = config.lab.presets.programs.nushell; 10 | in 11 | 12 | { 13 | options.lab.presets.programs.nushell = { 14 | enable = lib.mkEnableOption "Use nushell"; 15 | }; 16 | 17 | config.programs.nushell = lib.mkIf cfg.enable { 18 | enable = true; 19 | package = pkgs.unstable.nushell; 20 | extraConfig = lib.readFile ./config.nu; 21 | extraEnv = lib.readFile ./env.nu; 22 | }; 23 | } 24 | -------------------------------------------------------------------------------- /platforms/home-manager/modules/lab/presets/programs/nushell/env.nu: -------------------------------------------------------------------------------- 1 | $env.PROMPT_INDICATOR_VI_INSERT = { || "" } 2 | $env.PROMPT_INDICATOR_VI_NORMAL = { || "" } 3 | -------------------------------------------------------------------------------- /platforms/home-manager/modules/lab/presets/programs/starship.nix: -------------------------------------------------------------------------------- 1 | { 2 | lib, 3 | config, 4 | pkgs, 5 | ... 6 | }: 7 | 8 | let 9 | cfg = config.lab.presets.programs.starship; 10 | in 11 | 12 | { 13 | options.lab.presets.programs.starship = { 14 | enable = lib.mkEnableOption "Use starship prompt"; 15 | }; 16 | 17 | config.programs.starship = lib.mkIf cfg.enable { 18 | enable = true; 19 | package = pkgs.unstable.starship; 20 | enableNushellIntegration = true; 21 | 22 | settings = { 23 | add_newline = false; 24 | 25 | ### LEFT SIDE ### 26 | format = lib.concatStrings [ 27 | "$env_var" 28 | "$hostname" 29 | " " 30 | "$directory" 31 | " " 32 | "$character" 33 | ]; 34 | 35 | env_var.DATACENTER = { 36 | description = "Current datacenter"; 37 | format = "[$env_value.]($style)"; 38 | style = "dimmed white"; 39 | }; 40 | 41 | hostname = { 42 | ssh_only = false; 43 | ssh_symbol = ""; 44 | format = "[$hostname]($style)"; 45 | style = "white"; 46 | }; 47 | 48 | directory = { 49 | format = "[$path]($style)"; 50 | truncation_length = 1; 51 | style = "blue"; 52 | }; 53 | 54 | ### RIGHT SIDE ### 55 | right_format = "$time"; 56 | 57 | time = { 58 | format = "[$time]($style)"; 59 | style = "dimmed white"; 60 | utc_time_offset = "0"; 61 | disabled = false; 62 | }; 63 | }; 64 | }; 65 | } 66 | -------------------------------------------------------------------------------- /platforms/home-manager/modules/lab/profiles/default.nix: -------------------------------------------------------------------------------- 1 | { 2 | imports = [ ./fancy-shell.nix ]; 3 | } 4 | -------------------------------------------------------------------------------- /platforms/home-manager/modules/lab/profiles/fancy-shell.nix: -------------------------------------------------------------------------------- 1 | { lib, config, ... }: 2 | 3 | let 4 | cfg = config.lab.profiles.fancy-shell; 5 | in 6 | 7 | { 8 | options.lab.profiles.fancy-shell = { 9 | enable = lib.mkEnableOption "Configure a fancy login shell"; 10 | }; 11 | 12 | config.lab.presets = lib.mkIf cfg.enable { 13 | programs = { 14 | nushell.enable = lib.mkDefault true; 15 | starship.enable = lib.mkDefault true; 16 | }; 17 | }; 18 | } 19 | -------------------------------------------------------------------------------- /platforms/nixos/doc/default.nix: -------------------------------------------------------------------------------- 1 | { 2 | pkgs, 3 | lib, 4 | colmena, 5 | clapfile, 6 | home-manager, 7 | revision, 8 | ... 9 | }: 10 | 11 | let 12 | # The default module name assigned when traversing options. Without 13 | # evaluating `config` the name would otherwise be unknown. This causes mild 14 | # problems on options using an associative structure. 15 | # 16 | # Source: 17 | # https://github.com/NixOS/nixpkgs/blob/af8b9db5c00f1a8e4b83578acc578ff7d823b786/lib/types.nix#L858-L873 18 | defaultDocsModuleName = "‹name›"; 19 | 20 | # Evaluate modules in a minimal NixOS environment. This is lighter than 21 | # creating a new machine. 22 | toplevel = import (pkgs.path + "/nixos/lib/eval-config.nix") { 23 | modules = [ 24 | colmena.nixosModules.deploymentOptions 25 | colmena.nixosModules.assertionModule 26 | home-manager.nixosModules.home-manager 27 | clapfile.nixosModules.nixos 28 | ../modules 29 | 30 | { 31 | lab.networks.${defaultDocsModuleName}.ipv4.cidr = "127.0.0.1/32"; 32 | networking.hostName = "machine"; 33 | networking.domain = "example.com"; 34 | lab.host = { 35 | system = pkgs.system; 36 | ip4 = "127.0.0.1"; 37 | }; 38 | } 39 | ]; 40 | 41 | inherit (pkgs) system; 42 | inherit pkgs; 43 | }; 44 | 45 | allOptions = lib.optionAttrSetToDocList toplevel.options; 46 | 47 | # Only generate documentation for things under the `lab.*` namespace. 48 | filteredOptions = lib.filter ( 49 | opt: opt.visible && !opt.internal && (lib.hasPrefix "lab." opt.name) 50 | ) allOptions; 51 | 52 | # Convert to the format expected by `nixos-render-docs`. 53 | optionSet = lib.listToAttrs ( 54 | lib.flip map filteredOptions (opt: { 55 | inherit (opt) name; 56 | value = lib.attrsets.removeAttrs opt [ "name" ]; 57 | }) 58 | ); 59 | 60 | # Write the options file WITHOUT compiling mentioned dependencies. The 61 | # documentation generator will not traverse into listed store paths. 62 | optionsJSON = builtins.toFile "options.json" ( 63 | builtins.unsafeDiscardStringContext (builtins.toJSON optionSet) 64 | ); 65 | in 66 | { 67 | # To preview: 68 | # `man ./result/share/man/man5/lab.nix.5` 69 | manpage = 70 | pkgs.runCommand "generate-manpage-docs" 71 | { 72 | allowedReferences = [ "out" ]; 73 | buildInputs = [ 74 | pkgs.buildPackages.installShellFiles 75 | pkgs.buildPackages.nixos-render-docs 76 | ]; 77 | } 78 | '' 79 | mkdir -p $out/share/man/man5 80 | nixos-render-docs -j $NIX_BUILD_CORES options manpage \ 81 | --revision ${lib.escapeShellArg revision} \ 82 | --footer /dev/null \ 83 | ${optionsJSON} \ 84 | $out/share/man/man5/lab.nix.5 85 | ''; 86 | 87 | markdown = 88 | pkgs.runCommand "generate-markdown-docs" 89 | { 90 | allowedReferences = [ "out" ]; 91 | nativeBuildInputs = [ pkgs.nixos-render-docs ]; 92 | } 93 | '' 94 | mkdir $out 95 | nixos-render-docs -j $NIX_BUILD_CORES options commonmark \ 96 | --revision ${lib.escapeShellArg revision} \ 97 | --manpage-urls ${pkgs.path + "/doc/manpage-urls.json"} \ 98 | ${optionsJSON} \ 99 | $out/options.md 100 | ''; 101 | } 102 | -------------------------------------------------------------------------------- /platforms/nixos/modules/default.nix: -------------------------------------------------------------------------------- 1 | { imports = [ ./lab ]; } 2 | -------------------------------------------------------------------------------- /platforms/nixos/modules/lab/default.nix: -------------------------------------------------------------------------------- 1 | { lib, ... }: 2 | 3 | { 4 | imports = [ 5 | ./filesystems 6 | ./host.nix 7 | ./networks.nix 8 | ./profiles 9 | ./services 10 | ./ssh.nix 11 | ./stratums.nix 12 | ./system.nix 13 | ./virtualisation.nix 14 | ]; 15 | 16 | # A place to store constants. These should be set for every host. Not all 17 | # hosts need the same config, for example hosts in different datacenters may 18 | # have different configurations. 19 | options.lab = { 20 | domain = lib.mkOption { 21 | type = lib.types.str; 22 | example = "internal.cloud"; 23 | description = "Top-level domain for all hosts and datacenters"; 24 | }; 25 | 26 | datacenter = lib.mkOption { 27 | type = lib.types.str; 28 | example = "garage"; 29 | description = "Name of the datacenter. This becomes a subdomain."; 30 | }; 31 | }; 32 | } 33 | -------------------------------------------------------------------------------- /platforms/nixos/modules/lab/filesystems/default.nix: -------------------------------------------------------------------------------- 1 | { imports = [ ./zfs ]; } 2 | -------------------------------------------------------------------------------- /platforms/nixos/modules/lab/filesystems/zfs/.envrc: -------------------------------------------------------------------------------- 1 | use flake ".#$(git rev-parse --show-prefix)" 2 | -------------------------------------------------------------------------------- /platforms/nixos/modules/lab/filesystems/zfs/.gitignore: -------------------------------------------------------------------------------- 1 | # Optional: use this as the state file for local testing. 2 | state-file.json 3 | -------------------------------------------------------------------------------- /platforms/nixos/modules/lab/filesystems/zfs/default.nix: -------------------------------------------------------------------------------- 1 | { 2 | config, 3 | lib, 4 | pkgs, 5 | ... 6 | }: 7 | 8 | let 9 | inherit (lib) types mkOption concatMapStringsSep; 10 | 11 | cfg = config.lab.filesystems.zfs; 12 | 13 | # Example: "/mnt/tank" must be mounted before "/mnt/tank/library". 14 | # FS dependency order can be inferred by string length. 15 | topoSortedMounts = lib.pipe cfg.mounts [ 16 | lib.attrsToList 17 | (lib.sortOn (mount: lib.stringLength mount.name)) 18 | (map (mount: mount.name)) 19 | ]; 20 | 21 | nulib = lib.fileset.toSource { 22 | root = ./.; 23 | fileset = lib.fileset.fileFilter (f: f.hasExt "nu") ./.; 24 | }; 25 | 26 | # Expected property/settings for zpools and datasets. Managed by 27 | # `./propctl.nu`. 28 | zfsStateFile = { 29 | pools = lib.mapAttrs (_: pool: { 30 | ignored_properties = pool.unmanaged.settings; 31 | properties = pool.settings; 32 | }) cfg.pools; 33 | 34 | datasets = lib.pipe cfg.pools [ 35 | (lib.attrValues) 36 | 37 | (map (pool: [ 38 | # Declare pool properties 39 | { 40 | ${pool.name} = { 41 | ignored_properties = pool.unmanaged.properties; 42 | inherit (pool) properties; 43 | }; 44 | } 45 | 46 | # Declare dataset properties 47 | (lib.mapAttrs' (_: dataset: { 48 | name = "${pool.name}/${dataset.name}"; 49 | value = { 50 | ignored_properties = dataset.unmanaged.properties; 51 | inherit (dataset) properties; 52 | }; 53 | }) pool.datasets) 54 | ])) 55 | 56 | (lib.flatten) 57 | (lib.mergeAttrsList) 58 | ]; 59 | }; 60 | in 61 | { 62 | options.lab.filesystems.zfs = { 63 | enable = lib.mkEnableOption '' 64 | Mount and manage encrypted ZFS pools. This option changes the kernel and 65 | boot process. Reboot the machine after changing this option. 66 | 67 | ZFS requires some manual management (setup, decryption) so this module 68 | exposes a `system fs` command for administration tasks. 69 | 70 | Be aware that any services depending on ZFS datasets will fail to start 71 | until the datasets are decrypted and mounted. Defer services with 72 | `zfs.decryption.target`. 73 | ''; 74 | 75 | decryption = { 76 | name = mkOption { 77 | type = types.str; 78 | default = "zfs-decryption"; 79 | description = '' 80 | Name of the systemd decryption target (no trailing ".target") 81 | ''; 82 | }; 83 | 84 | target = mkOption { 85 | type = types.str; 86 | default = "${cfg.decryption.name}.target"; 87 | internal = true; 88 | description = "Name of the systemd decryption target"; 89 | }; 90 | }; 91 | 92 | mounts = mkOption { 93 | type = types.attrsOf types.str; 94 | default = { }; 95 | example = { 96 | "/mnt/tank" = "tank"; 97 | }; 98 | description = "Mapping of mount points to ZFS datasets"; 99 | }; 100 | 101 | # Since storage admin is potentially dangerous and occasionally requires 102 | # manual intervention (e.g. encryption keys), managing pools, datasets, 103 | # and properties in code is just as much an exercise in documentation as 104 | # execution. Things may fail. That's okay. At least you'll know how the 105 | # system should look. 106 | pools = mkOption { 107 | default = { }; 108 | description = '' 109 | Mapping of pool names to ZFS pool configurations. 110 | 111 | This option doesn't do anything by default. It adds administration 112 | commands that generate the pool, but for safety it only takes effect 113 | when run manually. 114 | ''; 115 | 116 | type = types.attrsOf ( 117 | types.submodule ( 118 | { name, ... }: 119 | { 120 | options.name = mkOption { 121 | type = types.str; 122 | example = "tank"; 123 | description = "Name of the ZFS pool"; 124 | default = name; 125 | }; 126 | 127 | options.settings = mkOption { 128 | type = types.attrsOf types.str; 129 | default = { }; 130 | description = '' 131 | Mapping of ZFS pool settings. See `zpoolprops(7)` for a list of 132 | available options. 133 | ''; 134 | 135 | example = { 136 | autoexpand = "on"; 137 | autotrim = "off"; 138 | }; 139 | }; 140 | 141 | options.properties = mkOption { 142 | type = types.attrsOf types.str; 143 | default = { }; 144 | description = '' 145 | Mapping of ZFS filesystem props to apply. See `zfsprops(7)` for 146 | a list of available options. 147 | ''; 148 | }; 149 | 150 | options.unmanaged = { 151 | settings = mkOption { 152 | type = types.listOf types.str; 153 | description = "Unmanaged zpool settings to ignore."; 154 | default = [ ]; 155 | }; 156 | 157 | properties = mkOption { 158 | type = types.listOf types.str; 159 | description = "Unmanaged dataset properties to ignore."; 160 | default = [ "nixos:shutdown-time" ]; 161 | }; 162 | }; 163 | 164 | options.vdevs = mkOption { 165 | default = [ ]; 166 | type = types.listOf ( 167 | types.submodule { 168 | options.type = mkOption { 169 | type = types.nullOr types.str; 170 | example = "mirror"; 171 | default = null; 172 | description = '' 173 | Type of virtual device. See `zpoolconcepts(7)` for a list of 174 | valid types. 175 | 176 | If set to `null`, the type is assumed to be a disk or file. 177 | ''; 178 | }; 179 | 180 | options.sources = mkOption { 181 | type = types.listOf types.str; 182 | example = [ "/dev/disk/by-label/wd-red-001" ]; 183 | description = '' 184 | List of block devices to use for the virtual device. The 185 | number of devices must match the type. 186 | 187 | It's recommended to specify block devices by their UUID or 188 | label to avoid overwriting to the wrong device. 189 | ''; 190 | }; 191 | } 192 | ); 193 | }; 194 | 195 | options.datasets = mkOption { 196 | default = { }; 197 | description = "Defines ZFS datasets to manage within a pool"; 198 | 199 | type = types.attrsOf ( 200 | types.submodule ( 201 | { name, ... }: 202 | { 203 | options.name = mkOption { 204 | type = types.str; 205 | example = "tank/library"; 206 | default = name; 207 | description = '' 208 | Name of the ZFS dataset. The name must be unique within the 209 | pool. 210 | ''; 211 | }; 212 | 213 | options.properties = mkOption { 214 | type = types.attrsOf types.str; 215 | default = { }; 216 | description = '' 217 | Mapping of ZFS dataset settings. See `zfsprops(7)` for a list 218 | of available options. 219 | ''; 220 | }; 221 | 222 | options.unmanaged.properties = mkOption { 223 | type = types.listOf types.str; 224 | description = "Unmanaged dataset properties to ignore."; 225 | default = [ ]; 226 | }; 227 | } 228 | ) 229 | ); 230 | }; 231 | } 232 | ) 233 | ); 234 | }; 235 | }; 236 | 237 | config = lib.mkIf cfg.enable { 238 | boot = { 239 | supportedFilesystems = [ "zfs" ]; 240 | 241 | # Disable the prompt for encryption credentials on boot. It blocks ssh, 242 | # and RPi3 boot loaders aren't expressive enough to securely run an SSH 243 | # server in stage 1. 244 | zfs.requestEncryptionCredentials = false; 245 | }; 246 | 247 | # This is used by other units to defer start until FS mounts are ready. 248 | systemd.targets.${cfg.decryption.name} = { 249 | description = "ZFS Dataset Decryption"; 250 | wants = [ "local-fs.target" ]; 251 | after = [ "local-fs.target" ]; 252 | }; 253 | 254 | lab.system.fs = { 255 | about = "ZFS management tools"; 256 | subcommands = { 257 | attach = { 258 | about = "Decrypt and mount ZFS datasets"; 259 | run = pkgs.writers.writeBash "attach-storage" '' 260 | set -euxo pipefail 261 | 262 | zfs load-key -a 263 | 264 | ${concatMapStringsSep "\n " (mountpoint: "mount ${mountpoint}") topoSortedMounts} 265 | 266 | systemctl start ${cfg.decryption.target} 267 | ''; 268 | }; 269 | 270 | detach = { 271 | about = "Unmount ZFS datasets"; 272 | run = pkgs.writers.writeBash "detach-storage" '' 273 | set -euxo pipefail 274 | 275 | systemctl stop ${cfg.decryption.target} 276 | 277 | ${concatMapStringsSep "\n " (mountpoint: "umount ${mountpoint}") ( 278 | lib.reverseList topoSortedMounts 279 | )} 280 | 281 | zfs unload-key -a 282 | ''; 283 | }; 284 | 285 | export-properties = { 286 | about = "Export known pool/dataset properties to a state file format"; 287 | run = pkgs.unstable.writers.writeNu "export-zfs-properties.nu" '' 288 | use ${nulib}/propctl.nu 289 | propctl export-system-state | to json 290 | ''; 291 | }; 292 | 293 | apply-properties = { 294 | about = "Manage ZFS dataset properties and pool attributes"; 295 | 296 | run = pkgs.unstable.writers.writeNu "manage-zfs-properties.nu" '' 297 | use ${nulib}/propctl.nu 298 | propctl plan | propctl apply 299 | ''; 300 | 301 | args = [ 302 | { 303 | id = "EXPECTED_STATE"; 304 | value_name = "FILE_PATH"; 305 | about = "Path to a JSON file containing the expected properties"; 306 | default_value = pkgs.writers.writeJSON "expected-state.json" zfsStateFile; 307 | } 308 | { 309 | id = "AUTO_CONFIRM"; 310 | about = "Automatically confirm changes"; 311 | short = "y"; 312 | long = "yes"; 313 | } 314 | ]; 315 | }; 316 | 317 | init = { 318 | about = "Create ZFS pools"; 319 | run = pkgs.writers.writeBash "init-storage" '' 320 | set -euxo pipefail 321 | 322 | # Create ZFS pools. 323 | ${lib.pipe cfg.pools [ 324 | (lib.attrValues) 325 | (map ( 326 | pool: 327 | "zpool create ${lib.escapeShellArg pool.name} ${ 328 | concatMapStringsSep " " ( 329 | vdev: lib.concatStringsSep " " ((if vdev.type != null then [ vdev.type ] else [ ]) ++ vdev.sources) 330 | ) pool.vdevs 331 | }" 332 | )) 333 | 334 | (lib.concatStringsSep "\n ") 335 | ]} 336 | 337 | # Create datasets. 338 | ${concatMapStringsSep "\n " ( 339 | pool: 340 | lib.pipe pool.datasets [ 341 | (lib.attrValues) 342 | (map (dataset: "zfs create ${lib.escapeShellArg "${pool.name}/${dataset.name}"}")) 343 | (lib.concatStringsSep "\n ") 344 | ] 345 | ) (lib.attrValues cfg.pools)} 346 | 347 | # Apply pool/dataset properties. 348 | system fs apply-properties --yes=true 349 | ''; 350 | }; 351 | }; 352 | }; 353 | 354 | fileSystems = lib.mapAttrs (mountpoint: dataset: { 355 | device = dataset; 356 | fsType = "zfs"; 357 | 358 | # Using `noauto` to prevent systemd from trying to mount the device at 359 | # boot, which fails because it is encrypted. The `system fs` command 360 | # will mount the device later. 361 | options = [ 362 | "zfsutil" 363 | "noauto" 364 | ]; 365 | }) cfg.mounts; 366 | }; 367 | } 368 | -------------------------------------------------------------------------------- /platforms/nixos/modules/lab/filesystems/zfs/develop.nix: -------------------------------------------------------------------------------- 1 | { pkgs, ... }: 2 | 3 | shell: { 4 | EXPECTED_STATE = "./state-file.json"; 5 | 6 | nativeBuildInputs = shell.nativeBuildInputs ++ [ 7 | (pkgs.unstable.writers.writeNuBin "unit-test" '' 8 | use ${pkgs.unstable.nushell.src}/crates/nu-std/testing.nu run-tests 9 | run-tests 10 | '') 11 | 12 | (pkgs.unstable.writers.writeNuBin "unit-test-watch" '' 13 | unit-test 14 | watch . --glob=*propctl.nu { 15 | clear 16 | unit-test 17 | } 18 | '') 19 | ]; 20 | } 21 | -------------------------------------------------------------------------------- /platforms/nixos/modules/lab/filesystems/zfs/propctl.nu: -------------------------------------------------------------------------------- 1 | # ZFS pools and datasets have properties, such as encryption or compression. 2 | # These properties should be initialized on creation and may change over time. 3 | # They are configured by command line. 4 | # 5 | # The goal of this module is to implement Configuration as Code for all ZFS 6 | # properties. You define the expected state and this module compares it 7 | # against the system, computes a diff, derives an execution plan, and 8 | # optionally brings the system into alignment. 9 | # 10 | # It should be run whenever new datasets/pools are created or when properties 11 | # update. 12 | # 13 | # NOTE: This program ignores pools and datasets not specified in the expected 14 | # state file. 15 | 16 | # Workaround for weird bug in newer nushell versions. Environment doesn't seem 17 | # to be loading correctly. Copied from the `std` module. 18 | export-env { 19 | $env.NU_LOG_FORMAT = $env.NU_LOG_FORMAT? | default "%ANSI_START%%DATE%|%LEVEL%|%MSG%%ANSI_STOP%" 20 | $env.NU_LOG_DATE_FORMAT = $env.NU_LOG_DATE_FORMAT? | default "%Y-%m-%dT%H:%M:%S%.3f" 21 | } 22 | 23 | use std log 24 | 25 | # Diff system state against expected state. 26 | export def plan []: nothing -> table { 27 | let state_file = open-state-file 28 | let actual = read-system-state 29 | let managed_state = filter-unmanaged-state $state_file $actual 30 | let expected_state = flatten-state-file $state_file 31 | generate-diff $managed_state $expected_state 32 | } 33 | 34 | # Apply a diff to the system, bringing it into alignment with the state file. 35 | # Expects output from `plan`. 36 | export def apply [--sudo]: table -> nothing { 37 | let diff = $in 38 | 39 | def ask_permission [] { 40 | if $env.AUTO_CONFIRM? == "true" { 41 | return true 42 | } 43 | 44 | print "Apply changes?" 45 | ([confirm cancel] | input list) == "confirm" 46 | } 47 | 48 | if ($diff | is-empty) { 49 | log info "No changes to apply." 50 | return 51 | } 52 | 53 | print ($diff | table --theme psql) 54 | 55 | if not (ask_permission) { 56 | log warning "Aborted." 57 | return 58 | } 59 | 60 | for plan in (to-execution-plan $diff) { 61 | let action = match $sudo { 62 | true => { cmd: sudo, args: [$plan.cmd ...$plan.args] } 63 | _ => $plan 64 | } 65 | 66 | log info $"Executing: ($action.cmd) ($action.args | str join ' ')" 67 | run-external $action.cmd ...$action.args 68 | } 69 | 70 | log info "Changes applied." 71 | } 72 | 73 | # Returns the actual state of all dataset properties and pool attributes on 74 | # the system. 75 | # 76 | # SEE: zprops(7), zpoolprops(7) 77 | export def read-system-state []: nothing -> table { 78 | let pools = zpool get all -H 79 | | lines 80 | | parse "{name}\t{prop}\t{value}\t{source}" 81 | | each { merge { type: pool } } 82 | | where source == local 83 | | where prop !~ 'feature@' # Not supported yet. 84 | 85 | let datasets = zfs get -Ht filesystem all 86 | | lines 87 | | parse "{name}\t{prop}\t{value}\t{source}" 88 | | each { merge { type: dataset } } 89 | | where source == local 90 | 91 | $pools | append $datasets 92 | } 93 | 94 | # Returns the expected state of all dataset properties and pool attributes as 95 | # specified in the state file, but flattened to a table matching the schema of 96 | # `read-system-state`. 97 | export def flatten-state-file [ 98 | state_file: record 99 | ]: nothing -> table { 100 | def enumerate_resources [resource_type: string, expected: record] { 101 | $expected 102 | | transpose name settings 103 | | each { merge { type: $resource_type } } 104 | | each { enumerate_properties } 105 | | flatten 106 | } 107 | 108 | def enumerate_properties []: record -> record { 109 | let resource = $in 110 | 111 | $resource.settings.properties 112 | | transpose name value 113 | | each {|prop| 114 | { 115 | type: $resource.type 116 | name: $resource.name 117 | prop: $prop.name 118 | value: $prop.value 119 | source: "local" 120 | } 121 | } 122 | } 123 | 124 | | enumerate_resources "pool" $state_file.pools 125 | | append (enumerate_resources "dataset" $state_file.datasets) 126 | } 127 | 128 | # Remove any pools, datasets, or properties from actual state that aren't 129 | # managed by the state file. 130 | export def filter-unmanaged-state [ 131 | state_file: record 132 | actual_state: table 133 | ] { 134 | $actual_state | filter {|entry| 135 | let expected = $state_file 136 | | get -is (match $entry.type { 137 | pool => "pools" 138 | dataset => "datasets" 139 | }) 140 | | get -is $entry.name 141 | 142 | # If the state file doesn't specify the pool/dataset, then ignore it. 143 | if $expected == null { 144 | false 145 | } else { 146 | not ($entry.prop in $expected.ignored_properties) 147 | } 148 | } 149 | } 150 | 151 | # Compare system state against expected state to find added, modified, or 152 | # removed properties. Works for both pools and datasets. 153 | export def generate-diff [ 154 | actual: table, 155 | expected: table, 156 | ]: nothing -> table { 157 | # Make comparison of expected <-> actual easier by indexing records by 158 | # composite key. 159 | def key_by_composite_id []: table -> record { 160 | let entries = $in 161 | 162 | # Transpose returns an empty table if the input is empty. Make sure we 163 | # always return a record. 164 | if ($entries | is-empty) { 165 | return {} 166 | } 167 | 168 | $entries 169 | | each {|entry| 170 | { 171 | key: (get_composite_id $entry) 172 | value: $entry 173 | } 174 | } 175 | | transpose -rd 176 | } 177 | 178 | def get_composite_id [entry: record]: nothing -> string { 179 | $"($entry.type):($entry.name):($entry.prop)" 180 | } 181 | 182 | let keyed_actual = $actual | key_by_composite_id 183 | let keyed_expected = $expected | key_by_composite_id 184 | 185 | # Find values in "expected" that do not exist in "actual", or values that 186 | # differ between the two. 187 | let additions_or_modifications = $expected | each {|entry| 188 | let actual = $keyed_actual 189 | | get -si (get_composite_id $entry) 190 | | get value? 191 | 192 | # Values are identical. No change needed. 193 | if ($entry.value == $actual) { 194 | return null 195 | } 196 | 197 | { 198 | type: $entry.type 199 | change: (match $actual { 200 | null => "add" 201 | _ => "modify" 202 | }) 203 | name: $entry.name 204 | prop: $entry.prop 205 | actual: $actual 206 | expected: $entry.value 207 | } 208 | } 209 | 210 | # Find values in "actual" that do not exist in "expected". 211 | let deletions = $actual | each {|entry| 212 | let expected = $keyed_expected | get -si (get_composite_id $entry) 213 | 214 | if $expected != null { 215 | return null 216 | } 217 | 218 | { 219 | type: $entry.type 220 | change: "remove" 221 | name: $entry.name 222 | prop: $entry.prop 223 | actual: $entry.value 224 | expected: null 225 | } 226 | } 227 | 228 | $additions_or_modifications 229 | | append $deletions 230 | | filter {|change| $change != null } 231 | | each { merge { sort_key: (get_composite_id $in) } } 232 | | sort-by sort_key 233 | | reject sort_key 234 | } 235 | 236 | # Generate commands from the state diff to bring the system into alignment. 237 | export def to-execution-plan [diff]: nothing -> list { 238 | let dataset_prop_changes = $diff 239 | | where type == dataset and change in [add, modify] 240 | | group-by name 241 | | items {|dataset, changes| 242 | let properties = $changes 243 | | each { [$in.prop $in.expected] | str join "=" } 244 | 245 | { 246 | cmd: "zfs" 247 | args: ["set" "-u" ...$properties $dataset] 248 | } 249 | } 250 | 251 | let dataset_prop_removals = $diff 252 | | where type == dataset and change == remove 253 | | each {|change| 254 | { 255 | cmd: "zfs" 256 | args: ["inherit" $change.prop $change.name] 257 | } 258 | } 259 | 260 | let pool_attr_changes = $diff 261 | | where type == pool and change in [add, modify] 262 | | each {|change| 263 | { 264 | cmd: "zpool" 265 | args: ["set" $"($change.prop)=($change.expected)" $change.name] 266 | } 267 | } 268 | 269 | | $dataset_prop_changes 270 | | append $dataset_prop_removals 271 | | append $pool_attr_changes 272 | } 273 | 274 | # Return the path to the JSON file specifying the expected system state. 275 | export def open-state-file []: nothing -> record { 276 | let config_file = if $env.EXPECTED_STATE? == null { 277 | error make { 278 | msg: "EXPECTED_STATE environment variable is required" 279 | } 280 | } else { 281 | $env.EXPECTED_STATE 282 | } 283 | 284 | open $config_file 285 | } 286 | 287 | # Generate a state file snapshot from current system state. 288 | export def export-system-state []: nothing -> record { 289 | let system_state = read-system-state 290 | 291 | def to_state_format []: table -> record { 292 | let resource_state = $in 293 | 294 | if ($resource_state | is-empty) { 295 | return {} 296 | } 297 | 298 | $resource_state 299 | | sort-by name 300 | | group-by name 301 | | items {|name, entries| 302 | { 303 | name: $name 304 | state: { 305 | ignored_properties: [] 306 | properties: ( 307 | $entries 308 | | sort-by prop 309 | | select prop value 310 | | transpose -rd 311 | ) 312 | } 313 | } 314 | } 315 | | transpose -rd 316 | } 317 | 318 | { 319 | pools: ($system_state | where type == pool | to_state_format), 320 | datasets: ($system_state | where type == dataset | to_state_format), 321 | } 322 | } 323 | -------------------------------------------------------------------------------- /platforms/nixos/modules/lab/filesystems/zfs/test_propctl.nu: -------------------------------------------------------------------------------- 1 | use std assert 2 | use propctl.nu 3 | 4 | # Generate a state file matching a strict schema given only partial data. 5 | def fill_partial_state [partial_state: record]: nothing -> record { 6 | def fill_missing_resource_fields [] { 7 | $in 8 | | default {} 9 | | transpose name state 10 | | upsert state {|| 11 | | default [] ignored_properties 12 | | default {} properties 13 | } 14 | | transpose -rd 15 | | into record # Transpose returns table for empty input 16 | } 17 | 18 | | $partial_state 19 | | upsert pools { fill_missing_resource_fields } 20 | | upsert datasets { fill_missing_resource_fields } 21 | } 22 | 23 | #[test] 24 | def test_flatten_empty_expected_state [] { 25 | let state = fill_partial_state {} 26 | let expected = propctl flatten-state-file $state 27 | 28 | assert equal $expected [] 29 | } 30 | 31 | #[test] 32 | def test_flatten_expected_state_with_dataset_properties [] { 33 | let state = fill_partial_state { 34 | datasets: { 35 | locker: { 36 | properties: { 37 | compression: on 38 | relatime: on 39 | } 40 | } 41 | } 42 | } 43 | 44 | let expected = propctl flatten-state-file $state 45 | 46 | assert equal $expected [ 47 | [type, name, prop, value, source]; 48 | [dataset, locker, compression, on, local] 49 | [dataset, locker, relatime, on, local] 50 | ] 51 | } 52 | 53 | #[test] 54 | def test_flatten_expected_state_with_pool_properties [] { 55 | let state = fill_partial_state { 56 | pools: { 57 | locker: { 58 | properties: { autoexpand: on } 59 | } 60 | } 61 | } 62 | 63 | let expected = propctl flatten-state-file $state 64 | 65 | assert equal $expected [ 66 | [type, name, prop, value, source]; 67 | [pool, locker, autoexpand, on, local] 68 | ] 69 | } 70 | 71 | #[test] 72 | def test_unmanaged_pools_and_datasets [] { 73 | let state = fill_partial_state { 74 | datasets: { 75 | locker: { 76 | properties: { 77 | compression: on 78 | } 79 | } 80 | } 81 | } 82 | 83 | let actual = [ 84 | [type, name, prop, value, source]; 85 | [dataset, locker, compression, on, local] 86 | [dataset, unmanaged, compression, on, local] 87 | [pool, unknown, autoexpand, on, local] 88 | ] 89 | 90 | let filtered = propctl filter-unmanaged-state $state $actual 91 | 92 | assert equal $filtered [ 93 | [type, name, prop, value, source]; 94 | [dataset, locker, compression, on, local] 95 | ] 96 | } 97 | 98 | #[test] 99 | def test_unmanaged_properties [] { 100 | let state = fill_partial_state { 101 | pools: { 102 | tank: { 103 | ignored_properties: [autotrim] 104 | properties: { 105 | autoexpand: on 106 | } 107 | } 108 | } 109 | datasets: { 110 | locker: { 111 | ignored_properties: [mountpoint] 112 | properties: { 113 | relatime: on 114 | } 115 | } 116 | } 117 | } 118 | 119 | let actual = [ 120 | [type, name, prop, value, source]; 121 | [dataset, locker, relatime, off, local] 122 | [dataset, locker, mountpoint, none, local] 123 | [pool, tank, autoexpand, off, local] 124 | [pool, tank, autotrim, off, local] 125 | ] 126 | 127 | let filtered = propctl filter-unmanaged-state $state $actual 128 | 129 | assert equal $filtered [ 130 | [type, name, prop, value, source]; 131 | [dataset, locker, relatime, off, local] 132 | [pool, tank, autoexpand, off, local] 133 | ] 134 | } 135 | 136 | #[test] 137 | def test_diff_added_properties [] { 138 | let expected = [ 139 | [type, name, prop, value, source]; 140 | [dataset, locker, compression, on, local] 141 | [dataset, locker, relatime, on, local] 142 | ] 143 | 144 | let actual = [] 145 | 146 | let diff = propctl generate-diff $actual $expected 147 | 148 | assert equal $diff [ 149 | [type, change, name, prop, actual, expected]; 150 | [dataset, add, locker, compression, null, on] 151 | [dataset, add, locker, relatime, null, on] 152 | ] 153 | } 154 | 155 | #[test] 156 | def test_diff_changed_properties [] { 157 | let expected = [ 158 | [type, name, prop, value, source]; 159 | [dataset, locker, compression, on, local] 160 | [dataset, locker, relatime, on, local] 161 | ] 162 | 163 | let actual = [ 164 | [type, name, prop, value, source]; 165 | [dataset, locker, compression, off, local] 166 | [dataset, locker, relatime, on, local] 167 | ] 168 | 169 | let diff = propctl generate-diff $actual $expected 170 | 171 | assert equal $diff [ 172 | [type, change, name, prop, actual, expected]; 173 | [dataset, modify, locker, compression, off, on] 174 | ] 175 | } 176 | 177 | #[test] 178 | def test_removed_properties [] { 179 | let expected = [ 180 | [type, name, prop, value, source]; 181 | [dataset, locker, compression, on, local] 182 | ] 183 | 184 | let actual = [ 185 | [type, name, prop, value, source]; 186 | [dataset, locker, compression, on, local] 187 | [dataset, locker, relatime, on, local] 188 | ] 189 | 190 | let diff = propctl generate-diff $actual $expected 191 | 192 | assert equal $diff [ 193 | [type, change, name, prop, actual, expected]; 194 | [dataset, remove, locker, relatime, on, null] 195 | ] 196 | } 197 | 198 | #[test] 199 | def test_diff_added_and_removed_properties [] { 200 | let expected = [ 201 | [type, name, prop, value, source]; 202 | [dataset, locker, compression, on, local] 203 | [pool, example, autoexpand, off, local] 204 | ] 205 | 206 | let actual = [ 207 | [type, name, prop, value, source]; 208 | [dataset, locker, relatime, on, local] 209 | [pool, example, autoexpand, on, local] 210 | ] 211 | 212 | let diff = propctl generate-diff $actual $expected 213 | 214 | assert equal $diff [ 215 | [type, change, name, prop, actual, expected]; 216 | [dataset, add, locker, compression, null, on] 217 | [dataset, remove, locker, relatime, on, null] 218 | [pool, modify, example, autoexpand, on, off] 219 | ] 220 | } 221 | 222 | #[test] 223 | def test_modified_dataset_execution_plan [] { 224 | let diff = [ 225 | [type, change, name, prop, actual, expected]; 226 | [dataset, add, locker, compression, null, on] 227 | [dataset, modify, locker, relatime, on, off] 228 | ] 229 | 230 | let commands = propctl to-execution-plan $diff 231 | 232 | assert equal $commands [ 233 | { cmd: zfs, args: [set -u compression=on relatime=off locker] } 234 | ] 235 | } 236 | 237 | #[test] 238 | def test_removed_dataset_props_execution_plan [] { 239 | let diff = [ 240 | [type, change, name, prop, actual, expected]; 241 | [dataset, remove, locker, relatime, on, null] 242 | [dataset, remove, locker, xattr, on, null] 243 | ] 244 | 245 | let commands = propctl to-execution-plan $diff 246 | 247 | assert equal $commands [ 248 | { cmd: zfs, args: [inherit relatime locker] } 249 | { cmd: zfs, args: [inherit xattr locker] } 250 | ] 251 | } 252 | 253 | #[test] 254 | def test_modified_pool_execution_plan [] { 255 | let diff = [ 256 | [type, change, name, prop, actual, expected]; 257 | [pool, modify, tank, autoexpand, off, on] 258 | [pool, add, tank, autotrim, null, on] 259 | ] 260 | 261 | let commands = propctl to-execution-plan $diff 262 | 263 | assert equal $commands [ 264 | { cmd: zpool, args: [set autoexpand=on tank] } 265 | { cmd: zpool, args: [set autotrim=on tank] } 266 | ] 267 | } 268 | -------------------------------------------------------------------------------- /platforms/nixos/modules/lab/host.nix: -------------------------------------------------------------------------------- 1 | { config, lib, ... }: 2 | 3 | # Exposes all the configuration options for the host. This is particularly 4 | # useful for the network address. 5 | 6 | let 7 | inherit (lib) types mkOption; 8 | cfg = config.lab.host; 9 | in 10 | 11 | { 12 | options.lab.host = { 13 | interface = mkOption { 14 | type = types.nullOr types.str; 15 | example = "eth0"; 16 | description = "Name of the primary network interface"; 17 | default = null; 18 | }; 19 | 20 | ip4 = mkOption { 21 | type = types.str; 22 | example = "192.168.1.10"; 23 | description = "IPv4 address for the primary network interface"; 24 | }; 25 | 26 | system = mkOption { 27 | type = types.enum lib.systems.doubles.all; 28 | example = "aarch64-linux"; 29 | description = "Architecture identifier of the host system"; 30 | }; 31 | 32 | profile = mkOption { 33 | type = types.deferredModule; 34 | description = "Module for hardware-specific configuration"; 35 | default = { }; 36 | }; 37 | 38 | module = mkOption { 39 | type = types.deferredModule; 40 | description = "Module for host-specific configuration"; 41 | }; 42 | 43 | publicKeys = mkOption { 44 | type = types.listOf types.str; 45 | default = [ ]; 46 | description = "Public keys associated with this host"; 47 | }; 48 | 49 | # Option names mirror `config.nix.buildMachines`. 50 | builder = { 51 | enable = lib.mkEnableOption "Use this machine as a remote builder"; 52 | 53 | uri = mkOption { 54 | type = types.str; 55 | description = "URI for the remote builder"; 56 | default = "ssh://${cfg.builder.sshUser}@${cfg.builder.hostName}"; 57 | }; 58 | 59 | hostName = mkOption { 60 | type = types.str; 61 | description = "The hostname of the remote builder"; 62 | default = config.networking.fqdn; 63 | }; 64 | 65 | systems = mkOption { 66 | type = types.listOf (types.enum lib.systems.doubles.all); 67 | description = "Systems supported by the builder"; 68 | default = [ cfg.system ]; 69 | }; 70 | 71 | sshUser = mkOption { 72 | type = types.str; 73 | description = "The username to log in as"; 74 | default = "root"; 75 | }; 76 | 77 | sshKey = mkOption { 78 | type = types.str; 79 | description = '' 80 | The path to the SSH private key with which to authenticate on the 81 | build machine. 82 | ''; 83 | 84 | # TODO: Find a better way to do this. 85 | default = "/root/.ssh/home_lab"; 86 | }; 87 | 88 | supportedFeatures = mkOption { 89 | type = types.listOf types.str; 90 | description = "System features supported by the builder"; 91 | default = [ ]; 92 | }; 93 | 94 | maxJobs = mkOption { 95 | type = types.int; 96 | description = "Maximum number of jobs to run in parallel"; 97 | default = 4; 98 | }; 99 | 100 | speedFactor = mkOption { 101 | type = types.int; 102 | description = "Speed factor for the builder"; 103 | default = 1; 104 | }; 105 | 106 | conf = mkOption { 107 | readOnly = true; 108 | description = '' 109 | Generated parameters for Nix to configure a remote builder. See: 110 | https://nixos.org/manual/nix/stable/advanced-topics/distributed-builds.html 111 | ''; 112 | 113 | default = lib.concatStringsSep " " [ 114 | cfg.builder.uri 115 | (lib.concatStringsSep "," cfg.builder.systems) 116 | cfg.builder.sshKey 117 | (toString cfg.builder.maxJobs) 118 | (toString cfg.builder.speedFactor) 119 | (lib.concatStringsSep "," cfg.builder.supportedFeatures) 120 | ]; 121 | }; 122 | }; 123 | }; 124 | } 125 | -------------------------------------------------------------------------------- /platforms/nixos/modules/lab/networks.nix: -------------------------------------------------------------------------------- 1 | { pkgs, lib, ... }: 2 | 3 | let 4 | inherit (lib) types mkOption; 5 | 6 | buildCidrInfo = pkgs.writers.writePython3 "print_cidr_info" { } '' 7 | from ipaddress import ip_interface 8 | import sys 9 | import json 10 | 11 | # Expects a CIDR address as the first argument. 12 | interface = ip_interface(sys.argv[1]) 13 | data = json.dumps({ 14 | "gatewayAddress": str(interface.ip), 15 | "networkAddress": str(interface.network.network_address), 16 | "broadcastAddress": str(interface.network.broadcast_address), 17 | "prefixLength": interface.network.prefixlen, 18 | "subnetMask": str(interface.network.netmask), 19 | "subnet": str(interface.network), 20 | }) 21 | 22 | print(data) 23 | ''; 24 | 25 | # Run the python script passing the CIDR address. Read the file back as 26 | # JSON, providing the data as a Nix value. 27 | parseCidrNotation = 28 | cidr_address: 29 | builtins.fromJSON ( 30 | builtins.readFile ( 31 | pkgs.runCommand "cidr-info" { inherit cidr_address; } '' 32 | ${buildCidrInfo} $cidr_address > $out 33 | '' 34 | ) 35 | ); 36 | 37 | networkOption = 38 | { config, name, ... }: 39 | let 40 | # WARN: `cidr` isn't set when evaluating documentation. Mark any derived 41 | # properties as `visible = false`. 42 | ipv4 = parseCidrNotation config.ipv4.cidr; 43 | in 44 | { 45 | options.name = mkOption { 46 | type = types.str; 47 | default = name; 48 | description = '' 49 | Unique name of the network. Be careful changing it - some modules 50 | use it as persistent state. 51 | ''; 52 | }; 53 | 54 | options.ipv4 = { 55 | cidr = mkOption { 56 | description = '' 57 | Defines the subnet in CIDR notation. The IP address is the 58 | gatway. 59 | 60 | Syntax: "{gateway_ip}/{mask_bits}" 61 | 62 | Other fields are generated from this data for convenience. 63 | ''; 64 | 65 | type = types.str; 66 | example = "192.168.1.1/24"; 67 | }; 68 | 69 | gateway = mkOption { 70 | description = "IP address of the gateway for this network"; 71 | type = types.str; 72 | default = ipv4.gatewayAddress; 73 | example = "192.168.1.1"; 74 | visible = false; 75 | readOnly = true; 76 | }; 77 | 78 | network = mkOption { 79 | description = "First available IP address in the network"; 80 | type = types.str; 81 | default = ipv4.networkAddress; 82 | example = "192.168.1.0"; 83 | visible = false; 84 | readOnly = true; 85 | }; 86 | 87 | broadcast = mkOption { 88 | description = "Broadcast address for the network"; 89 | type = types.str; 90 | default = ipv4.broadcastAddress; 91 | example = "192.168.1.255"; 92 | visible = false; 93 | readOnly = true; 94 | }; 95 | 96 | prefixLength = mkOption { 97 | description = "Number of bits in the network mask"; 98 | type = types.int; 99 | default = ipv4.prefixLength; 100 | example = 24; 101 | visible = false; 102 | readOnly = true; 103 | }; 104 | 105 | netmask = mkOption { 106 | description = "Subnet mask for this network"; 107 | type = types.str; 108 | default = ipv4.subnetMask; 109 | example = "255.255.255.0"; 110 | visible = false; 111 | readOnly = true; 112 | }; 113 | 114 | subnet = mkOption { 115 | description = "CIDR notation of the subnet"; 116 | type = types.str; 117 | default = ipv4.subnet; 118 | example = "192.168.1.0/24"; 119 | visible = false; 120 | readOnly = true; 121 | }; 122 | 123 | dhcp.pools = mkOption { 124 | description = "Assignable address ranges used by DHCP"; 125 | default = [ ]; 126 | type = types.listOf ( 127 | types.submodule { 128 | options.start = mkOption { 129 | type = types.str; 130 | description = "Starting range for DHCP"; 131 | example = "192.168.1.10"; 132 | }; 133 | 134 | options.end = mkOption { 135 | type = types.str; 136 | description = "Ending range for DHCP"; 137 | example = "192.168.1.254"; 138 | }; 139 | } 140 | ); 141 | }; 142 | }; 143 | }; 144 | in 145 | { 146 | options.lab.networks = mkOption { 147 | description = '' 148 | A description of every network in the lab. This is used to generate 149 | subnets and routing rules in lower-level modules. It must be 150 | consistently defined for every host in the lab. 151 | ''; 152 | 153 | type = types.attrsOf (types.submoduleWith { modules = [ networkOption ]; }); 154 | default = { }; 155 | }; 156 | } 157 | -------------------------------------------------------------------------------- /platforms/nixos/modules/lab/profiles/default.nix: -------------------------------------------------------------------------------- 1 | { 2 | imports = [ 3 | ./file-server.nix 4 | ./observability 5 | ./router.nix 6 | ./vpn 7 | ]; 8 | } 9 | -------------------------------------------------------------------------------- /platforms/nixos/modules/lab/profiles/file-server.nix: -------------------------------------------------------------------------------- 1 | { 2 | config, 3 | pkgs, 4 | lib, 5 | ... 6 | }: 7 | 8 | let 9 | inherit (config.lab) domain; 10 | inherit (config.lab.filesystems.zfs) decryption pools; 11 | cfg = config.lab.profiles.file-server; 12 | in 13 | { 14 | options.lab.profiles.file-server = { 15 | enable = lib.mkEnableOption "Run a file server"; 16 | }; 17 | 18 | config = lib.mkIf cfg.enable { 19 | deployment.tags = [ "file-server" ]; 20 | 21 | lab.filesystems.zfs = { 22 | enable = true; 23 | mounts = { 24 | "/mnt/pool0" = "pool0"; 25 | "/mnt/pool0/syncthing" = "pool0/syncthing"; 26 | }; 27 | 28 | pools.pool0 = { 29 | vdevs = [ 30 | { 31 | type = "raidz1"; 32 | sources = [ 33 | "sda" 34 | "sdb" 35 | "sdc" 36 | ]; 37 | } 38 | ]; 39 | 40 | properties = { 41 | xattr = "sa"; 42 | acltype = "posixacl"; 43 | atime = "off"; 44 | encryption = "aes-256-gcm"; 45 | keyformat = "passphrase"; 46 | compression = "on"; 47 | mountpoint = "none"; 48 | }; 49 | 50 | datasets.syncthing.properties."com.sun:auto-snapshot" = "true"; 51 | }; 52 | }; 53 | 54 | systemd.services.syncthing = { 55 | requires = [ decryption.target ]; 56 | after = [ decryption.target ]; 57 | 58 | # Don't start automatically. Wait for pool decryption. 59 | wantedBy = lib.mkForce [ decryption.target ]; 60 | }; 61 | 62 | services = { 63 | syncthing = { 64 | enable = true; 65 | package = pkgs.unstable.syncthing; 66 | dataDir = "/mnt/pool0/syncthing"; 67 | configDir = "/mnt/pool0/syncthing/.config"; 68 | 69 | settings = { 70 | options.urAccepted = 3; 71 | gui.theme = "dark"; 72 | 73 | folders."/mnt/pool0/syncthing/attic" = { 74 | id = "attic"; 75 | devices = [ 76 | "laptop" 77 | "phone" 78 | ]; 79 | label = "Attic"; 80 | }; 81 | 82 | devices = { 83 | laptop = { 84 | addresses = [ "tcp://ava.host.${domain}" ]; 85 | id = "JPX6IWF-HZIA465-YNSYU4H-YTHKJL6-CO3KN66-EKMNT7O-7DBTGWI-V6ICAQN"; 86 | }; 87 | 88 | phone = { 89 | addresses = [ "dynamic" ]; 90 | id = "S2U7KKV-SXJGOI3-6MSJWIT-U2JP32Y-HH7WZU5-ZDS6KAT-6CNYRAM-ZQTWZAQ"; 91 | }; 92 | }; 93 | }; 94 | }; 95 | 96 | zfs = { 97 | autoSnapshot = { 98 | enable = true; 99 | flags = "-kp --utc"; 100 | }; 101 | 102 | autoScrub = { 103 | enable = true; 104 | pools = lib.attrNames pools; 105 | }; 106 | }; 107 | }; 108 | 109 | networking.firewall = { 110 | allowedTCPPorts = [ 22000 ]; # TCP Sync 111 | allowedUDPPorts = [ 112 | 22000 113 | 21027 114 | ]; # QUIC + LAN Discovery 115 | }; 116 | }; 117 | } 118 | -------------------------------------------------------------------------------- /platforms/nixos/modules/lab/profiles/observability/default.nix: -------------------------------------------------------------------------------- 1 | { 2 | nodes, 3 | config, 4 | lib, 5 | ... 6 | }: 7 | 8 | # WIP: Playing around with Grafana's observability tools. 9 | 10 | let 11 | cfg = config.lab.profiles.observability; 12 | in 13 | 14 | { 15 | options.lab.profiles.observability = { 16 | enable = lib.mkEnableOption "Enable observability services"; 17 | }; 18 | 19 | config = lib.mkIf cfg.enable { 20 | services = { 21 | prometheus = { 22 | enable = true; 23 | retentionTime = "1y"; 24 | port = 9090; 25 | 26 | globalConfig = { 27 | scrape_interval = "15s"; 28 | scrape_timeout = "10s"; 29 | evaluation_interval = "30s"; 30 | }; 31 | 32 | # TODO: Make this a preset. Generate scrapers. 33 | exporters.node = { 34 | enable = true; 35 | enabledCollectors = [ "systemd" ]; 36 | port = 9100; 37 | listenAddress = "rpi4-002.nova.vpn.selfhosted.city"; 38 | }; 39 | 40 | scrapeConfigs = [ 41 | { 42 | job_name = "prometheus"; 43 | static_configs = [ 44 | { 45 | targets = [ "localhost:9090" ]; 46 | labels = { 47 | instance = "prometheus"; 48 | }; 49 | } 50 | ]; 51 | } 52 | 53 | { 54 | job_name = "coredns"; 55 | static_configs = [ 56 | { 57 | targets = [ "${nodes.rpi4-001.config.networking.fqdn}:9153" ]; 58 | labels = { 59 | instance = "coredns"; 60 | }; 61 | } 62 | ]; 63 | } 64 | 65 | { 66 | job_name = "node"; 67 | static_configs = [ 68 | { 69 | targets = [ "localhost:9100" ]; 70 | labels = { 71 | instance = "localhost"; 72 | }; 73 | } 74 | ]; 75 | } 76 | ]; 77 | }; 78 | 79 | grafana = { 80 | enable = true; 81 | 82 | provision = { 83 | enable = true; 84 | datasources.settings.datasources = [ 85 | { 86 | name = "Prometheus"; 87 | type = "prometheus"; 88 | access = "proxy"; 89 | url = "http://localhost:9090"; 90 | } 91 | ]; 92 | }; 93 | }; 94 | }; 95 | }; 96 | } 97 | -------------------------------------------------------------------------------- /platforms/nixos/modules/lab/profiles/router.nix: -------------------------------------------------------------------------------- 1 | { 2 | config, 3 | lib, 4 | pkgs, 5 | nodes, 6 | ... 7 | }: 8 | 9 | let 10 | inherit (config.lab.services.gateway.networks) 11 | home 12 | iot 13 | guest 14 | datacenter 15 | ; 16 | 17 | inherit (config.lab.services.gateway) wan; 18 | inherit (config.lab.services) discovery; 19 | cfg = config.lab.profiles.router; 20 | json = pkgs.formats.json { }; 21 | 22 | xbox = { 23 | ip4 = "10.0.2.250"; 24 | ports = { 25 | tcp = [ 3074 ]; 26 | udp = [ 27 | 3074 28 | 3075 29 | 88 30 | 500 31 | 3544 32 | 4500 33 | ]; 34 | }; 35 | }; 36 | 37 | networks = { 38 | datacenter.interface = "lan"; # Dongle to ethernet switch 39 | home.interface = "wap"; # Dongle to WAP (no VLAN) 40 | iot.interface = "vlan-iot"; 41 | work.interface = "vlan-work"; 42 | guest.interface = "vlan-guest"; 43 | }; 44 | in 45 | { 46 | options.lab.profiles.router = { 47 | enable = lib.mkEnableOption '' 48 | Turn this device into a router. 49 | 50 | The network interface names MUST match the ones configured in 51 | `router.networks`. Configure them with udev before enabling this 52 | profile. 53 | ''; 54 | }; 55 | 56 | config = lib.mkIf cfg.enable { 57 | deployment.tags = [ "router" ]; 58 | 59 | environment.systemPackages = [ 60 | pkgs.unstable.bottom # System load observer 61 | pkgs.unstable.conntrack-tools # Inspect active connection states 62 | pkgs.unstable.doggo # DNS testing 63 | pkgs.unstable.tcpdump # Inspect traffic (used with Wireshark) 64 | config.services.etcd.package # For probing dynamic DNS records 65 | ]; 66 | 67 | # VLANs are sent by the WAP (a UniFi U6 Lite). 68 | networking.vlans = { 69 | vlan-iot = { 70 | id = 10; 71 | interface = home.interface; 72 | }; 73 | 74 | vlan-work = { 75 | id = 20; 76 | interface = home.interface; 77 | }; 78 | 79 | vlan-guest = { 80 | id = 30; 81 | interface = home.interface; 82 | }; 83 | }; 84 | 85 | # Bridge mDNS between my IoT, Guest, and Home LAN. 86 | services.avahi = { 87 | enable = true; 88 | reflector = true; 89 | openFirewall = false; 90 | nssmdns4 = true; 91 | allowInterfaces = [ 92 | iot.interface 93 | home.interface 94 | guest.interface 95 | ]; 96 | }; 97 | 98 | networking = { 99 | # Don't use DNS servers advertised by the ISP. 100 | inherit (config.lab.services.dhcp) nameservers; 101 | }; 102 | 103 | lab.services = { 104 | gateway = { 105 | enable = true; 106 | wan.interface = "wan"; # Dongle to WAN 107 | networks = networks; 108 | }; 109 | 110 | # Powers host and service discovery. 111 | discovery.server = { 112 | enable = true; 113 | dns.zone = "${config.lab.datacenter}.${config.lab.domain}"; 114 | 115 | static-values = [ 116 | { 117 | key = "${discovery.server.dns.prefix.host.key}/${config.networking.hostName}"; 118 | value = json.generate "host-record.json" { 119 | host = config.lab.host.ip4; 120 | type = "A"; 121 | # use default TTL 122 | }; 123 | } 124 | ]; 125 | 126 | allowInterfaces = [ 127 | home.interface 128 | datacenter.interface 129 | ]; 130 | }; 131 | 132 | dhcp = { 133 | enable = true; 134 | networks = networks; 135 | 136 | discovery = { 137 | enable = true; 138 | dns.prefix = discovery.server.dns.prefix.host.key; 139 | }; 140 | 141 | # NOTE: DNS IP address may be in a different subnet. This still 142 | # depends on the gateway to forward traffic. 143 | nameservers = lib.pipe nodes [ 144 | (lib.filterAttrs (_: node: node.config.lab.services.dns.enable)) 145 | (lib.mapAttrsToList (_: node: node.config.lab.host.ip4)) 146 | ]; 147 | 148 | reservations = [ 149 | { 150 | type = "hw-address"; 151 | id = "C4:CB:76:8A:C3:D7"; 152 | ip-address = xbox.ip4; 153 | } 154 | ]; 155 | }; 156 | 157 | dns = { 158 | enable = true; 159 | interfaces = lib.mapAttrsToList (_: net: net.interface) networks; 160 | server.id = config.networking.fqdn; 161 | hosts.file = "${pkgs.unstable.stevenblack-blocklist}/hosts"; 162 | zone.name = "host.${config.lab.domain}"; 163 | prometheus.enable = true; 164 | 165 | discovery = { 166 | enable = true; 167 | dns.prefix = "/${discovery.server.dns.prefix.name}"; 168 | 169 | zones = [ 170 | "host.${discovery.server.dns.zone}" 171 | ]; 172 | }; 173 | 174 | forward = [ 175 | { 176 | zone = "."; 177 | tls = { 178 | ip = "1.1.1.1"; 179 | servername = "cloudflare-dns.com"; 180 | }; 181 | } 182 | ]; 183 | }; 184 | }; 185 | 186 | # Although not technically part of the home lab, this is still my home 187 | # router and some networking requirements are bound to bleed over. 188 | networking = { 189 | # Open ports for multiplayer gaming on Xbox Live. 190 | nat.forwardPorts = lib.flatten ( 191 | lib.mapAttrsToList ( 192 | proto: ports: 193 | lib.forEach ports (port: { 194 | inherit proto; 195 | sourcePort = port; 196 | destination = "${xbox.ip4}:${toString port}"; 197 | }) 198 | ) xbox.ports 199 | ); 200 | 201 | firewall.interfaces = 202 | let 203 | mdns = [ 5353 ]; 204 | in 205 | { 206 | # Allow IoT devices to be discoverable from the Home LAN. 207 | ${iot.interface}.allowedUDPPorts = mdns; 208 | ${home.interface}.allowedUDPPorts = mdns; 209 | 210 | ${wan.interface} = { 211 | allowedUDPPorts = xbox.ports.udp; 212 | allowedTCPPorts = xbox.ports.tcp; 213 | }; 214 | }; 215 | }; 216 | }; 217 | } 218 | -------------------------------------------------------------------------------- /platforms/nixos/modules/lab/profiles/vpn/client.nix: -------------------------------------------------------------------------------- 1 | { config, lib, ... }: 2 | 3 | let 4 | cfg = config.lab.profiles.vpn.client; 5 | in 6 | 7 | { 8 | options.lab.profiles.vpn.client = { 9 | enable = lib.mkEnableOption '' 10 | Enable the VPN client. 11 | 12 | This requires manual setup. The first time the host comes online, run: 13 | $ tailscale up --auth-key --login-server 14 | ''; 15 | }; 16 | 17 | config = lib.mkIf cfg.enable { 18 | services.tailscale.enable = true; 19 | 20 | # TODO: Provision the key and init the VPN automatically. 21 | # See `authKeyParameters` and `authKeyFile`. 22 | }; 23 | } 24 | -------------------------------------------------------------------------------- /platforms/nixos/modules/lab/profiles/vpn/default.nix: -------------------------------------------------------------------------------- 1 | { 2 | imports = [ 3 | ./client.nix 4 | ./server.nix 5 | ]; 6 | } 7 | -------------------------------------------------------------------------------- /platforms/nixos/modules/lab/profiles/vpn/server.nix: -------------------------------------------------------------------------------- 1 | { config, lib, ... }: 2 | 3 | let 4 | inherit (config.lab) datacenter domain; 5 | inherit (config.lab.services.vpn.server.listen) port; 6 | cfg = config.lab.profiles.vpn.server; 7 | in 8 | 9 | { 10 | options.lab.profiles.vpn.server = { 11 | enable = lib.mkEnableOption '' 12 | Run a VPN server on this host. 13 | ''; 14 | }; 15 | 16 | config = lib.mkIf cfg.enable { 17 | lab.services.vpn.server = { 18 | enable = true; 19 | 20 | url = "http://${config.networking.hostName}.host.${datacenter}.${domain}:${toString port}"; 21 | dns.zone = "${datacenter}.vpn.${domain}"; 22 | openFirewall = true; 23 | 24 | listen = { 25 | address = "0.0.0.0"; 26 | port = 8080; 27 | }; 28 | }; 29 | 30 | # Experimental: Expose the VPN server through a Cloudflare Tunnel. 31 | # TODO: Move this to a separate module and route services by VPN/ACL. 32 | # 33 | # TODO: Find a workaround for `POST` WebSocket upgrades. The tailscale 34 | # client initiates handshakes with `POST`, but this is not RFC-compliant. 35 | # CF Tunnels strip the upgrade headers and the handshake fails. 36 | # https://github.com/cloudflare/cloudflared/issues/883 37 | services.cloudflared = { 38 | enable = true; 39 | 40 | # Depends on Cloudflare for TLS termination. This is a security risk, 41 | # but considering the reputational damage to Cloudflare if they MITM'd 42 | # it, it's low on my list of concerns. 43 | # 44 | # NOTE: The default certificate only works for immediate subdomains. 45 | tunnels.vpn = { 46 | credentialsFile = config.age.secrets.vpn-tunnel-key.path; 47 | default = "http_status:404"; 48 | ingress = { 49 | "vpn.${domain}" = { 50 | service = "http://localhost:${toString port}"; 51 | }; 52 | }; 53 | }; 54 | }; 55 | 56 | age.secrets.vpn-tunnel-key = { 57 | file = ./vpn-tunnel-key.age; 58 | group = config.services.cloudflared.group; 59 | owner = config.services.cloudflared.user; 60 | }; 61 | }; 62 | } 63 | -------------------------------------------------------------------------------- /platforms/nixos/modules/lab/profiles/vpn/vpn-tunnel-key.age: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PsychoLlama/home-lab/1efe2b73a4bed94ccabf38895be23d80a9016d2a/platforms/nixos/modules/lab/profiles/vpn/vpn-tunnel-key.age -------------------------------------------------------------------------------- /platforms/nixos/modules/lab/services/default.nix: -------------------------------------------------------------------------------- 1 | { 2 | imports = [ 3 | ./dhcp 4 | ./discovery 5 | ./dns.nix 6 | ./gateway.nix 7 | ./vpn 8 | ]; 9 | } 10 | -------------------------------------------------------------------------------- /platforms/nixos/modules/lab/services/dhcp/default.nix: -------------------------------------------------------------------------------- 1 | { 2 | config, 3 | lib, 4 | pkgs, 5 | ... 6 | }: 7 | 8 | let 9 | inherit (lib) types mkOption; 10 | 11 | cfg = config.lab.services.dhcp; 12 | kea = pkgs.kea; # Not configurable outside nixpkgs. 13 | json = pkgs.formats.json { }; 14 | 15 | # Enrich `cfg.networks` with data from `lab.networks`. 16 | networks = lib.mapAttrs ( 17 | _: network: network // { inherit (config.lab.networks.${network.id}) ipv4; } 18 | ) cfg.networks; 19 | in 20 | 21 | { 22 | options.lab.services.dhcp = { 23 | enable = lib.mkEnableOption "Run a DHCP server"; 24 | networks = mkOption { 25 | description = "DHCP server configuration per interface"; 26 | default = { }; 27 | type = types.attrsOf ( 28 | types.submodule ( 29 | { name, ... }: 30 | { 31 | options.interface = mkOption { 32 | type = types.str; 33 | description = "Network interface to bind"; 34 | }; 35 | 36 | options.id = mkOption { 37 | type = types.enum (lib.attrNames config.lab.networks); 38 | description = "One of `lab.networks`"; 39 | default = name; 40 | }; 41 | } 42 | ) 43 | ); 44 | }; 45 | 46 | discovery = { 47 | enable = lib.mkEnableOption "Sync DHCP leases with etcd"; 48 | 49 | dns.prefix = lib.mkOption { 50 | type = types.str; 51 | description = '' 52 | Etcd key prefix where DNS records are stored. 53 | Uses reverse scheme, e.g. `/dns/com/example/subdomain`. 54 | ''; 55 | }; 56 | }; 57 | 58 | nameservers = mkOption { 59 | type = types.listOf types.str; 60 | description = "DNS servers advertised to clients"; 61 | default = [ ]; 62 | example = [ 63 | "1.1.1.1" 64 | "9.9.9.9" 65 | ]; 66 | }; 67 | 68 | reservations = mkOption { 69 | type = types.listOf ( 70 | types.submodule { 71 | options.type = mkOption { 72 | type = types.enum [ 73 | "hw-address" 74 | "client-id" 75 | ]; 76 | 77 | description = "Reservation type"; 78 | default = "client-id"; 79 | }; 80 | 81 | options.id = mkOption { 82 | type = types.str; 83 | description = "DHCP reservation identifier"; 84 | }; 85 | 86 | options.ip-address = mkOption { 87 | type = types.str; 88 | description = "IP address to assign to the host"; 89 | }; 90 | } 91 | ); 92 | 93 | description = "Static DHCP reservations"; 94 | default = [ ]; 95 | }; 96 | 97 | lib.toSubnetId = mkOption { 98 | type = types.functionTo types.int; 99 | readOnly = true; 100 | description = '' 101 | Convert a network ID to a subnet ID by hashing the network name. 102 | ''; 103 | 104 | default = 105 | networkName: 106 | lib.pipe networkName [ 107 | (builtins.hashString "md5") 108 | 109 | # Trim to avoid exceeding the max integer size of 2^32. 110 | (lib.substring 0 7) 111 | 112 | # Hashes are hex encoded. 113 | lib.fromHexString 114 | 115 | # Subnet IDs must never be zero. 116 | (builtins.add 1) 117 | ]; 118 | }; 119 | 120 | lib.toClientId = mkOption { 121 | type = types.functionTo types.str; 122 | readOnly = true; 123 | description = '' 124 | Convert an IPv4 address to a DHCP client identifier. Useful when you 125 | want to "hard-code" the IP but keep the router, DNS, and other fields 126 | dynamic. 127 | 128 | Outputs a hex string for compatibility with Kea. 129 | ''; 130 | 131 | default = 132 | ip4: 133 | lib.pipe ip4 [ 134 | # ["127", "0", "0", "1"] 135 | (lib.splitString ".") 136 | 137 | # [127, 0, 0, 1] 138 | (lib.map lib.strings.toInt) 139 | 140 | # ["7F", "0", "0", "1"] 141 | (lib.map lib.trivial.toHexString) 142 | 143 | # ["7F", "00", "00", "01"] 144 | (lib.map (lib.strings.fixedWidthString 2 "0")) 145 | 146 | # "7F:00:00:01" 147 | (lib.concatStringsSep ":") 148 | 149 | # "FE:01:7F:00:00:01" 150 | (id: "FE:01:${id}") 151 | ]; 152 | }; 153 | }; 154 | 155 | config = lib.mkIf cfg.enable { 156 | # Open DHCP ports on participating LAN interfaces. 157 | networking.firewall.interfaces = lib.mapAttrs' (_: network: { 158 | name = network.interface; 159 | value.allowedUDPPorts = [ 67 ]; 160 | }) networks; 161 | 162 | services.kea = { 163 | dhcp4 = { 164 | enable = true; 165 | settings = { 166 | valid-lifetime = 3600; 167 | renew-timer = 900; 168 | rebind-timer = 1800; 169 | 170 | hooks-libraries = lib.mkIf cfg.discovery.enable [ 171 | { 172 | library = "${kea}/lib/kea/hooks/libdhcp_run_script.so"; 173 | parameters = { 174 | sync = false; # Non-blocking script 175 | name = pkgs.unstable.writers.writeNu "sync-records-to-etcd.nu" { 176 | makeWrapperArgs = [ 177 | # Add etcd to the path 178 | "--set" 179 | "PATH" 180 | (lib.makeBinPath [ 181 | config.services.etcd.package 182 | ]) 183 | 184 | # Pass options to the script. 185 | "--set" 186 | "SETTINGS" 187 | (json.generate "settings.json" { 188 | etcd_prefix = cfg.discovery.dns.prefix; 189 | }) 190 | ]; 191 | } ./sync-records-to-etcd.nu; 192 | }; 193 | } 194 | ]; 195 | 196 | lease-database = { 197 | type = "memfile"; 198 | persist = true; 199 | name = "/var/lib/kea/dhcp4.leases"; 200 | }; 201 | 202 | interfaces-config = { 203 | dhcp-socket-type = "raw"; 204 | interfaces = lib.mapAttrsToList (_: network: network.interface) networks; 205 | }; 206 | 207 | subnet4 = lib.map (network: { 208 | id = cfg.lib.toSubnetId network.id; 209 | subnet = network.ipv4.subnet; 210 | pools = lib.forEach network.ipv4.dhcp.pools (lease: { 211 | pool = "${lease.start} - ${lease.end}"; 212 | }); 213 | 214 | option-data = 215 | (lib.optionals (cfg.nameservers != [ ]) [ 216 | { 217 | name = "domain-name-servers"; 218 | data = lib.concatStringsSep ", " cfg.nameservers; 219 | } 220 | ]) 221 | ++ [ 222 | { 223 | name = "routers"; 224 | data = network.ipv4.gateway; 225 | } 226 | { 227 | name = "broadcast-address"; 228 | data = network.ipv4.broadcast; 229 | } 230 | ]; 231 | }) (lib.attrValues networks); 232 | 233 | host-reservation-identifiers = [ 234 | "hw-address" 235 | "client-id" 236 | ]; 237 | 238 | reservations-global = true; 239 | reservations-in-subnet = true; 240 | reservations-out-of-pool = false; 241 | reservations = map (reservation: { 242 | inherit (reservation) ip-address; 243 | ${reservation.type} = reservation.id; 244 | }) cfg.reservations; 245 | }; 246 | }; 247 | }; 248 | }; 249 | } 250 | -------------------------------------------------------------------------------- /platforms/nixos/modules/lab/services/dhcp/sync-records-to-etcd.nu: -------------------------------------------------------------------------------- 1 | use std/log 2 | 3 | # Passed in from nix. 4 | let settings = open $env.SETTINGS 5 | 6 | # Synchronize DHCP leases to etcd when they change. 7 | def main [event: string] { 8 | log info $"DHCP event ($event) hostname=($env.LEASE4_HOSTNAME? | default '?')" 9 | 10 | # This is the only event handled currently. 11 | if $event != "leases4_committed" { 12 | return 13 | } 14 | 15 | let count_removed = $env.DELETED_LEASES4_SIZE | into int 16 | let count_added = $env.LEASES4_SIZE | into int 17 | log info $"Leases changed added=($count_added) removed=($count_removed)" 18 | 19 | let removed = seq 1 $count_removed | enumerate | each {|item| 20 | { 21 | hostname: ($env | get $"DELETED_LEASES4_AT($item.index)_HOSTNAME") 22 | } 23 | } 24 | 25 | let added = seq 1 $count_added | enumerate | each {|item| 26 | { 27 | hostname: ($env | get $"LEASES4_AT($item.index)_HOSTNAME") 28 | ip: ($env | get $"LEASES4_AT($item.index)_ADDRESS") 29 | } 30 | } 31 | 32 | $removed | each { remove_lease $in } 33 | $added | each { add_lease $in } 34 | 35 | log info $"Leases synchronized" 36 | } 37 | 38 | # Add a record to etcd. 39 | def add_lease [lease] { 40 | if ($lease.hostname | is-empty) { 41 | log info $"Ignoring lease with empty hostname ip=($lease.ip)" 42 | } 43 | 44 | let etcd_key = make_etcd_key $lease.hostname 45 | let record = { host: $lease.ip } | to json --raw 46 | 47 | log info $"Adding record to etcd ip=($lease.ip) key=($etcd_key)" 48 | etcdctl put $etcd_key $record 49 | } 50 | 51 | # Remove a record from etcd. 52 | def remove_lease [lease] { 53 | let etcd_key = make_etcd_key $lease.hostname 54 | 55 | log info $"Removing record from etcd key=($etcd_key)" 56 | etcdctl del $etcd_key 57 | } 58 | 59 | # Find the right etcd key for the DNS record 60 | def make_etcd_key [hostname: string] { 61 | $"($settings.etcd_prefix)/($hostname)" 62 | } 63 | -------------------------------------------------------------------------------- /platforms/nixos/modules/lab/services/discovery/default.nix: -------------------------------------------------------------------------------- 1 | { 2 | imports = [ 3 | ./server.nix 4 | ]; 5 | } 6 | -------------------------------------------------------------------------------- /platforms/nixos/modules/lab/services/discovery/server.nix: -------------------------------------------------------------------------------- 1 | { 2 | config, 3 | lib, 4 | pkgs, 5 | ... 6 | }: 7 | 8 | let 9 | inherit (lib) types; 10 | etcd = config.services.etcd.package; 11 | cfg = config.lab.services.discovery.server; 12 | ports = { 13 | client = 2379; 14 | }; 15 | in 16 | 17 | { 18 | options.lab.services.discovery.server = { 19 | enable = lib.mkEnableOption "Run a discovery server"; 20 | 21 | allowInterfaces = lib.mkOption { 22 | type = types.listOf types.str; 23 | default = [ ]; 24 | description = '' 25 | Non-firewalled network interfaces allowed to reach etcd. 26 | ''; 27 | }; 28 | 29 | dns = { 30 | zone = lib.mkOption { 31 | type = types.str; 32 | example = "subdomain.example.com"; 33 | description = '' 34 | Root-level zone where DNS records are kept. Works with subdomains. 35 | ''; 36 | }; 37 | 38 | prefix = { 39 | name = lib.mkOption { 40 | type = types.str; 41 | default = "dns"; 42 | example = "/dns/com/example"; 43 | description = '' 44 | Key that follows the DNS prefix for indexing individual records. 45 | ''; 46 | }; 47 | 48 | key = lib.mkOption { 49 | type = types.str; 50 | readOnly = true; 51 | description = '' 52 | Generated full path to the DNS prefix key in etcd. 53 | ''; 54 | 55 | default = lib.pipe "${cfg.dns.zone}.${cfg.dns.prefix.name}." [ 56 | # [ "example" "com" "dns" "" ] 57 | (lib.splitString ".") 58 | 59 | # [ "" "dns" "com" "example" ] 60 | (lib.reverseList) 61 | 62 | # "/dns/com/example" 63 | (lib.concatStringsSep "/") 64 | ]; 65 | }; 66 | 67 | host = { 68 | name = lib.mkOption { 69 | type = types.str; 70 | default = "host"; 71 | description = '' 72 | Key that follows the DNS prefix for indexing individual hosts. 73 | ''; 74 | }; 75 | 76 | key = lib.mkOption { 77 | type = types.str; 78 | readOnly = true; 79 | example = "/dns/com/example/hosts"; 80 | description = '' 81 | Generated full prefix key for host records in etcd. 82 | ''; 83 | 84 | default = "${cfg.dns.prefix.key}/${cfg.dns.prefix.host.name}"; 85 | }; 86 | }; 87 | }; 88 | }; 89 | 90 | static-values = lib.mkOption { 91 | default = [ ]; 92 | description = '' 93 | Values to add every time the discovery server starts. 94 | 95 | WARNING: Old values are not removed. They must be purged manually. 96 | ''; 97 | 98 | type = types.listOf ( 99 | types.submodule ( 100 | { config, ... }: 101 | { 102 | options.key = lib.mkOption { 103 | type = types.str; 104 | example = "/arbitrary/key"; 105 | description = '' 106 | Where to store the data. 107 | ''; 108 | }; 109 | 110 | options.value = lib.mkOption { 111 | type = types.either types.str types.path; 112 | example = "value"; 113 | description = '' 114 | Arbitrary value to store. 115 | ''; 116 | 117 | apply = value: if lib.isString value then pkgs.writeText "etcd-content" value else value; 118 | }; 119 | 120 | options.command = lib.mkOption { 121 | type = types.str; 122 | readOnly = true; 123 | description = '' 124 | Generated command that updates etcd. 125 | ''; 126 | 127 | default = '' 128 | ${etcd}/bin/etcdctl put -- ${config.key} < ${config.value} 129 | ''; 130 | }; 131 | } 132 | ) 133 | ); 134 | }; 135 | }; 136 | 137 | # TODO: 138 | # - Add a client service daemon that performs healthchecks and updates etcd. 139 | config = lib.mkIf cfg.enable { 140 | services.etcd = { 141 | enable = true; 142 | package = pkgs.etcd; 143 | extraConf = { 144 | # Bind to all interfaces. Highly discouraged, but it's my LAN. 145 | LISTEN_CLIENT_URLS = "http://0.0.0.0:${toString ports.client}"; 146 | }; 147 | }; 148 | 149 | # Add static values as soon as the service is ready. 150 | systemd.services.etcd.postStart = 151 | lib.concatMapStringsSep "\n" (lib.getAttr "command") 152 | cfg.static-values; 153 | 154 | # Open the firewall for etcd. 155 | networking.firewall.interfaces = lib.genAttrs cfg.allowInterfaces (_: { 156 | allowedTCPPorts = [ ports.client ]; 157 | }); 158 | }; 159 | } 160 | -------------------------------------------------------------------------------- /platforms/nixos/modules/lab/services/dns.nix: -------------------------------------------------------------------------------- 1 | { 2 | config, 3 | lib, 4 | pkgs, 5 | ... 6 | }: 7 | 8 | let 9 | inherit (lib) types mkOption; 10 | cfg = config.lab.services.dns; 11 | forward = 12 | upstream: 13 | 14 | let 15 | inherit (upstream) method zone; 16 | inherit (upstream) tls udp; 17 | in 18 | 19 | lib.getAttr method ({ 20 | tls = '' 21 | forward ${zone} tls://${tls.ip} { 22 | tls_servername ${tls.servername} 23 | health_check 1h 24 | } 25 | ''; 26 | 27 | udp = '' 28 | forward ${zone} ${udp.ip} { 29 | health_check 1h 30 | } 31 | ''; 32 | 33 | "resolv.conf" = '' 34 | forward ${zone} /etc/resolv.conf 35 | ''; 36 | }); 37 | in 38 | 39 | { 40 | options.lab.services.dns = { 41 | enable = lib.mkEnableOption "Run a DNS server"; 42 | 43 | interfaces = mkOption { 44 | type = types.listOf types.str; 45 | default = [ ]; 46 | description = '' 47 | Network interfaces to listen on. Loopback is always enabled. 48 | ''; 49 | }; 50 | 51 | ttl = mkOption { 52 | type = types.str; 53 | default = "60"; 54 | description = "Default TTL for DNS records"; 55 | }; 56 | 57 | server.id = mkOption { 58 | type = types.nullOr types.str; 59 | default = null; 60 | description = '' 61 | Name Server Identifier. This is sent with `OPT` records. It is 62 | especially useful in HA setups to disambiguate the server. 63 | 64 | Note: At the moment, only the `dig` client supports this feature. 65 | ''; 66 | }; 67 | 68 | prometheus = { 69 | enable = lib.mkEnableOption "Enable Prometheus metrics exporter"; 70 | port = mkOption { 71 | type = types.int; 72 | default = 9153; 73 | description = "Port to expose Prometheus metrics"; 74 | }; 75 | }; 76 | 77 | discovery = { 78 | enable = lib.mkEnableOption "Use etcd for service discovery"; 79 | zones = mkOption { 80 | type = types.listOf types.str; 81 | default = [ ]; 82 | description = '' 83 | DNS zones to resolve using the service discovery mechanism. 84 | ''; 85 | }; 86 | 87 | dns.prefix = lib.mkOption { 88 | type = types.str; 89 | description = '' 90 | Etcd key prefix where DNS records are stored. 91 | Uses reverse scheme, e.g. `/dns/com/example/subdomain`. 92 | ''; 93 | }; 94 | }; 95 | 96 | forward = mkOption { 97 | default = [ ]; 98 | description = '' 99 | Forward DNS queries to other DNS servers. This is useful for resolving 100 | external domains or for using a DNS-over-TLS service. 101 | 102 | ORDER MATTERS. The first matching zone is used even if a more specific 103 | zone is later in the list. 104 | ''; 105 | 106 | type = types.listOf ( 107 | types.submodule { 108 | options.zone = mkOption { 109 | type = types.str; 110 | description = '' 111 | The domain pattern being forwarded. Use . to 112 | match all queries. 113 | ''; 114 | }; 115 | 116 | options.method = mkOption { 117 | type = types.enum [ 118 | "tls" 119 | "udp" 120 | "resolv.conf" 121 | ]; 122 | 123 | default = "tls"; 124 | description = '' 125 | Method used to resolve queries for this zone. TLS is 126 | recommended. 127 | ''; 128 | }; 129 | 130 | options.tls = { 131 | ip = mkOption { 132 | type = types.str; 133 | description = "IP address of the DNS server"; 134 | }; 135 | 136 | servername = mkOption { 137 | type = types.str; 138 | description = "Hostname used for session validation"; 139 | }; 140 | }; 141 | 142 | options.udp.ip = mkOption { 143 | type = types.str; 144 | description = "IP address of the DNS server"; 145 | }; 146 | } 147 | ); 148 | }; 149 | 150 | zone = { 151 | name = mkOption { 152 | type = types.nullOr types.str; 153 | example = "dc1.example.com"; 154 | default = null; 155 | description = '' 156 | The DNS zone to serve. This is the domain name for which the server 157 | is authoritative. It is used to generate the zone file. 158 | ''; 159 | }; 160 | 161 | file = mkOption { 162 | type = types.path; 163 | default = pkgs.unstable.writeText "local.zone" '' 164 | $ORIGIN ${cfg.zone.name}. 165 | @ IN SOA dns trash ( 166 | 1 ; Version number 167 | 60 ; Zone refresh interval 168 | 30 ; Zone update retry timeout 169 | 180 ; Zone TTL 170 | 3600) ; Negative response TTL 171 | 172 | ; Custom records 173 | ${lib.concatMapStrings (record: '' 174 | ${record.name} ${record.ttl} IN ${record.type} ${record.value} 175 | '') cfg.zone.records} 176 | ''; 177 | 178 | description = '' 179 | Path to a BIND zone file. Setting this option will override 180 | the generated config. 181 | ''; 182 | }; 183 | 184 | records = mkOption { 185 | type = types.listOf ( 186 | types.submodule { 187 | options.type = mkOption { 188 | type = types.str; 189 | description = "The type of DNS record to create"; 190 | }; 191 | 192 | options.name = mkOption { 193 | type = types.str; 194 | description = '' 195 | Any BIND zone record identifier, usually a subdomain name. 196 | Use @ for apex records. 197 | 198 | Note: Only domains within the lab's zone are recognized. 199 | ''; 200 | }; 201 | 202 | options.value = mkOption { 203 | type = types.str; 204 | description = "IP addresses pointing to the service"; 205 | }; 206 | 207 | options.ttl = mkOption { 208 | type = types.str; 209 | description = "Length of time in seconds to cache the record"; 210 | default = cfg.ttl; 211 | }; 212 | } 213 | ); 214 | 215 | description = "Insert custom DNS records"; 216 | default = [ ]; 217 | }; 218 | }; 219 | 220 | hosts.file = mkOption { 221 | type = types.path; 222 | default = pkgs.emptyFile; 223 | description = '' 224 | An `/etc/hosts` structured file mapping domain names to IP addresses. 225 | Can be used with `pkgs.stevenblack-blocklist` for DNS-level adblock. 226 | ''; 227 | }; 228 | }; 229 | 230 | config = lib.mkIf cfg.enable { 231 | networking = { 232 | # Ignore advertised DNS servers and resolve queries locally. In HA 233 | # setups, other nameservers may be unresponsive. 234 | nameservers = [ "127.0.0.1" ]; 235 | 236 | # Expose DNS+UDP (no TCP support). 237 | firewall.interfaces = lib.genAttrs cfg.interfaces (_: { 238 | allowedUDPPorts = [ 53 ]; 239 | allowedTCPPorts = lib.mkIf cfg.prometheus.enable [ 240 | cfg.prometheus.port 241 | ]; 242 | }); 243 | }; 244 | 245 | services.coredns = { 246 | enable = true; 247 | package = pkgs.unstable.coredns; 248 | 249 | config = '' 250 | (common) { 251 | bind ${toString ([ "lo" ] ++ cfg.interfaces)} 252 | 253 | log 254 | errors 255 | local 256 | 257 | ${lib.optionalString (cfg.server.id != null) "nsid ${cfg.server.id}"} 258 | } 259 | 260 | . { 261 | import common 262 | cache ${cfg.ttl} 263 | 264 | ${lib.optionalString cfg.prometheus.enable '' 265 | prometheus 0.0.0.0:${toString cfg.prometheus.port} 266 | ''} 267 | 268 | ${lib.optionalString (cfg.zone.name != null) '' 269 | # WARN: This takes full control of whatever zone it's given. 270 | # There is no fallthrough. It will fight you. 271 | file ${cfg.zone.file} ${cfg.zone.name} { 272 | reload 0 273 | } 274 | ''} 275 | 276 | ${lib.optionalString cfg.discovery.enable '' 277 | etcd ${toString cfg.discovery.zones} { 278 | path ${cfg.discovery.dns.prefix} 279 | fallthrough 280 | } 281 | ''} 282 | 283 | hosts ${cfg.hosts.file} { 284 | fallthrough 285 | reload 0 286 | ttl ${cfg.ttl} 287 | } 288 | 289 | # Upstream DNS servers 290 | ${lib.concatMapStringsSep "\n" forward cfg.forward} 291 | } 292 | ''; 293 | }; 294 | }; 295 | } 296 | -------------------------------------------------------------------------------- /platforms/nixos/modules/lab/services/gateway.nix: -------------------------------------------------------------------------------- 1 | { config, lib, ... }: 2 | 3 | let 4 | inherit (lib) types mkOption; 5 | 6 | cfg = config.lab.services.gateway; 7 | 8 | # Enrich `cfg.networks` with data from `lab.networks`. 9 | networks = lib.mapAttrs ( 10 | _: network: network // { inherit (config.lab.networks.${network.id}) ipv4; } 11 | ) cfg.networks; 12 | in 13 | 14 | { 15 | options.lab.services.gateway = { 16 | enable = lib.mkEnableOption "Run a NAT gateway and firewall"; 17 | 18 | networks = mkOption { 19 | description = "Map of networks to create from `lab.networks`"; 20 | default = { }; 21 | type = types.attrsOf ( 22 | types.submodule ( 23 | { name, ... }: 24 | { 25 | options = { 26 | id = mkOption { 27 | description = "One of `lab.networks`"; 28 | type = types.enum (lib.attrNames config.lab.networks); 29 | default = name; 30 | }; 31 | 32 | interface = mkOption { 33 | description = "Name of the network interface to use"; 34 | type = types.str; 35 | }; 36 | }; 37 | } 38 | ) 39 | ); 40 | }; 41 | 42 | wan.interface = mkOption { 43 | type = types.str; 44 | description = "WAN interface"; 45 | }; 46 | }; 47 | 48 | config = lib.mkIf cfg.enable { 49 | networking = { 50 | useDHCP = false; 51 | 52 | interfaces = lib.mkMerge [ 53 | { 54 | # Get a public IP from the WAN link, presumably an ISP. 55 | ${cfg.wan.interface}.useDHCP = lib.mkDefault true; 56 | } 57 | 58 | # Statically assign the gateway IP to all managed LAN interfaces. 59 | (lib.mapAttrs' (_: network: { 60 | name = network.interface; 61 | value = { 62 | useDHCP = false; 63 | ipv4.addresses = [ 64 | { 65 | address = network.ipv4.gateway; 66 | prefixLength = network.ipv4.prefixLength; 67 | } 68 | ]; 69 | }; 70 | }) networks) 71 | ]; 72 | 73 | nat = { 74 | enable = true; 75 | externalInterface = cfg.wan.interface; 76 | internalInterfaces = lib.mapAttrsToList (_: network: network.interface) networks; 77 | 78 | internalIPs = lib.mapAttrsToList ( 79 | _: network: "${network.ipv4.gateway}/${toString network.ipv4.prefixLength}" 80 | ) networks; 81 | }; 82 | 83 | # Expose SSH to all LAN interfaces. 84 | firewall.interfaces = lib.mapAttrs' (_: network: { 85 | name = network.interface; 86 | value.allowedTCPPorts = [ 22 ]; 87 | }) networks; 88 | }; 89 | 90 | # SSH should not be accessible from the open internet. 91 | services.openssh.openFirewall = lib.mkDefault false; 92 | 93 | # Enable strict reverse path filtering. This guards against some forms of 94 | # IP spoofing. 95 | boot.kernel.sysctl = lib.mkMerge [ 96 | { 97 | # Enable for the WAN interface. 98 | "net.ipv4.conf.default.rp_filter" = lib.mkDefault 1; 99 | "net.ipv4.conf.${cfg.wan.interface}.rp_filter" = lib.mkDefault 1; 100 | } 101 | 102 | # Enable for all LAN interfaces. 103 | (lib.mapAttrs' (_: network: { 104 | name = "net.ipv4.conf.${network.interface}.rp_filter"; 105 | value = lib.mkDefault 1; 106 | }) networks) 107 | ]; 108 | }; 109 | } 110 | -------------------------------------------------------------------------------- /platforms/nixos/modules/lab/services/vpn/default.nix: -------------------------------------------------------------------------------- 1 | { 2 | imports = [ 3 | ./server.nix 4 | ]; 5 | } 6 | -------------------------------------------------------------------------------- /platforms/nixos/modules/lab/services/vpn/server.nix: -------------------------------------------------------------------------------- 1 | { config, lib, ... }: 2 | 3 | let 4 | cfg = config.lab.services.vpn.server; 5 | in 6 | 7 | { 8 | options.lab.services.vpn.server = { 9 | enable = lib.mkEnableOption '' 10 | Use the Headscale VPN coordination server. 11 | 12 | This provides automatic DNS and ACLs restricting privileged lab services 13 | to authorized clients. 14 | ''; 15 | 16 | url = lib.mkOption { 17 | type = lib.types.str; 18 | description = '' 19 | Advertised URL that VPN clients can reach. 20 | ''; 21 | }; 22 | 23 | openFirewall = lib.mkOption { 24 | type = lib.types.bool; 25 | default = true; 26 | description = '' 27 | Open the firewall to allow traffic to the VPN server. 28 | ''; 29 | }; 30 | 31 | listen = { 32 | address = lib.mkOption { 33 | type = lib.types.str; 34 | default = "localhost"; 35 | description = '' 36 | Address to listen on. 37 | ''; 38 | }; 39 | 40 | port = lib.mkOption { 41 | type = lib.types.int; 42 | default = 8080; 43 | description = '' 44 | Port to listen on. 45 | ''; 46 | }; 47 | }; 48 | 49 | dns.zone = lib.mkOption { 50 | type = lib.types.str; 51 | default = "vpn.internal"; 52 | description = '' 53 | Domain under which hostnames are resolved. Every VPN client shows up 54 | here indexed by hostname. 55 | ''; 56 | }; 57 | }; 58 | 59 | config = lib.mkIf cfg.enable { 60 | networking.firewall.allowedTCPPorts = lib.mkIf cfg.openFirewall [ cfg.listen.port ]; 61 | deployment.tags = [ "vpn" ]; 62 | 63 | services.headscale = { 64 | enable = true; 65 | settings = { 66 | server_url = cfg.url; 67 | listen_addr = "${cfg.listen.address}:${toString cfg.listen.port}"; 68 | dns.base_domain = cfg.dns.zone; 69 | logtail.enabled = lib.mkDefault true; 70 | }; 71 | }; 72 | }; 73 | } 74 | -------------------------------------------------------------------------------- /platforms/nixos/modules/lab/ssh.nix: -------------------------------------------------------------------------------- 1 | { config, lib, ... }: 2 | 3 | let 4 | cfg = config.lab.ssh; 5 | in 6 | 7 | { 8 | options.lab.ssh = { 9 | enable = lib.mkEnableOption "Enable SSH access"; 10 | 11 | authorizedKeys = lib.mkOption { 12 | type = lib.types.listOf lib.types.path; 13 | default = [ ]; 14 | description = '' 15 | SSH keys which are allowed root access. 16 | ''; 17 | }; 18 | }; 19 | 20 | config = lib.mkIf cfg.enable { 21 | users.users.root.openssh.authorizedKeys.keyFiles = cfg.authorizedKeys; 22 | 23 | services.openssh = { 24 | enable = true; 25 | settings.PasswordAuthentication = false; 26 | }; 27 | }; 28 | } 29 | -------------------------------------------------------------------------------- /platforms/nixos/modules/lab/stratums.nix: -------------------------------------------------------------------------------- 1 | { config, lib, ... }: 2 | 3 | # The lab is divided into "Stratums", which are sets of services that can only 4 | # depend on themselves or a lower stratum. This helps prevent circular 5 | # dependencies. Circular dependencies cause substantial problems when 6 | # rebuilding the lab from scratch. 7 | # 8 | # The lower stratums should contain as few services as possible. 9 | 10 | let 11 | inherit (lib) types mkOption; 12 | inherit (cfg) platform framework; 13 | cfg = config.lab.stratums; 14 | 15 | require = 16 | name: stratum: 17 | mkOption { 18 | type = types.anything; 19 | description = "Convenience option for asserting a stratum dependency"; 20 | internal = true; 21 | readOnly = true; 22 | default = { 23 | assertion = stratum.initialized; 24 | message = "This service requires the ${name} stratum."; 25 | }; 26 | }; 27 | in 28 | 29 | { 30 | options.lab.stratums = { 31 | platform = { 32 | initialized = mkOption { 33 | type = types.bool; 34 | default = false; 35 | description = '' 36 | Whether all critical services have been initialized. This stratum 37 | includes the lowest-level services, such as networking and 38 | discovery. 39 | ''; 40 | }; 41 | }; 42 | 43 | framework = { 44 | assertion = require "platform" platform.initialized; 45 | initialized = mkOption { 46 | type = types.bool; 47 | default = false; 48 | description = '' 49 | Whether all multi-tenant supports have been initialized. This 50 | stratum includes services like container orchestration, service 51 | discovery, and key management. 52 | ''; 53 | }; 54 | }; 55 | 56 | application = { 57 | assertion = require "framework" (framework.initialized && platform.initialized); 58 | initialized = mkOption { 59 | type = types.bool; 60 | default = false; 61 | description = '' 62 | Whether all tenant-level services have been initialized. This level 63 | includes everything that is not crucial to platform or framework 64 | stratums, such as applications, servers, and databases. 65 | 66 | There are no higher stratums. Nothing should depend on this level. 67 | ''; 68 | }; 69 | }; 70 | }; 71 | 72 | config.assertions = [ 73 | { 74 | assertion = cfg.framework.initialized -> cfg.platform.initialized; 75 | message = "Platform stratum must be initialized before framework."; 76 | } 77 | { 78 | assertion = cfg.application.initialized -> cfg.framework.initialized; 79 | message = "Framework stratum must be initialized before applications."; 80 | } 81 | ]; 82 | } 83 | -------------------------------------------------------------------------------- /platforms/nixos/modules/lab/system.nix: -------------------------------------------------------------------------------- 1 | { 2 | lib, 3 | pkgs, 4 | config, 5 | ... 6 | }: 7 | 8 | let 9 | toml = pkgs.formats.toml { }; 10 | in 11 | { 12 | options.lab.system = lib.mkOption { 13 | description = "Subcommands for the `system` administration command"; 14 | type = toml.type; 15 | default = { }; 16 | }; 17 | 18 | config.clapfile = { 19 | enable = lib.mkDefault true; 20 | 21 | command = { 22 | name = "system"; 23 | description = "System administration commands"; 24 | subcommands = config.lab.system; 25 | }; 26 | }; 27 | } 28 | -------------------------------------------------------------------------------- /platforms/nixos/modules/lab/virtualisation.nix: -------------------------------------------------------------------------------- 1 | { 2 | config, 3 | lib, 4 | pkgs, 5 | ... 6 | }: 7 | 8 | let 9 | inherit (lib) types; 10 | cfg = config.lab.virtualisation; 11 | 12 | # Generate a unique name that does not exceed the 15 character limit. 13 | mkIfname = name: "mv-${lib.substring 0 12 (builtins.hashString "md5" name)}"; 14 | in 15 | 16 | { 17 | options.lab.virtualisation = { 18 | sharedModules = lib.mkOption { 19 | type = types.listOf types.deferredModule; 20 | default = [ ]; 21 | description = '' 22 | Default NixOS modules to include in all containers. 23 | ''; 24 | }; 25 | 26 | containers = lib.mkOption { 27 | type = types.attrsOf types.deferredModule; 28 | default = { }; 29 | 30 | description = '' 31 | Defines a set of NixOS Containers that start automatically. 32 | 33 | Because NixOS containers evaluate in a new blank NixOS namespace and 34 | there's no way to extend that namespace automatically, this provides 35 | a workaround by generating a module with all the imports and baseline 36 | configuration defined. 37 | 38 | The generated container uses macvlan networking to allow a private 39 | network namespace that can still communicate with the outside world. 40 | It assumes the host is also using macvlan networking, otherwise hosts 41 | and containers cannot communicate. 42 | 43 | WARNING: This does not work over WiFi because 802.11 only supports 44 | communicating with a single MAC address. 45 | ''; 46 | }; 47 | }; 48 | 49 | config = { 50 | # Define host-level networking so the container can communicate with 51 | # the outside world. 52 | networking = lib.mkMerge ( 53 | lib.mapAttrsToList (containerName: _: { 54 | interfaces.${mkIfname containerName}.useDHCP = lib.mkDefault true; 55 | 56 | macvlans.${mkIfname containerName} = { 57 | mode = "bridge"; 58 | interface = lib.mkDefault config.lab.host.interface; 59 | }; 60 | }) cfg.containers 61 | ); 62 | 63 | containers = lib.mapAttrs (containerName: containerConfig: { 64 | autoStart = lib.mkDefault true; 65 | privateNetwork = lib.mkDefault true; 66 | 67 | macvlans = [ 68 | "${config.lab.host.interface}:bridge" 69 | ]; 70 | 71 | # The NixOS Containers implementation evaluates a whole new NixOS module 72 | # namespace for each container. This isn't a typical config. 73 | config = { 74 | imports = cfg.sharedModules ++ [ containerConfig ]; 75 | 76 | config = { 77 | nixpkgs.pkgs = lib.mkDefault pkgs; 78 | system.stateVersion = lib.mkDefault config.system.stateVersion; 79 | 80 | networking = { 81 | useDHCP = false; 82 | hostName = lib.mkDefault containerName; 83 | interfaces.bridge.useDHCP = lib.mkDefault true; 84 | 85 | # Since this registers as a new host on the network it must be 86 | # namespaced to avoid conflicts with other instances. 87 | dhcpcd.extraConfig = '' 88 | hostname ${containerName}-${config.networking.hostName} 89 | ''; 90 | }; 91 | }; 92 | }; 93 | }) cfg.containers; 94 | }; 95 | } 96 | -------------------------------------------------------------------------------- /platforms/nixos/tests/default.nix: -------------------------------------------------------------------------------- 1 | { 2 | pkgs, 3 | callPackage, 4 | colmena, 5 | clapfile, 6 | home-manager, 7 | agenix, 8 | }: 9 | 10 | let 11 | baseModule = { 12 | defaults = { 13 | imports = [ 14 | colmena.nixosModules.deploymentOptions 15 | colmena.nixosModules.assertionModule 16 | home-manager.nixosModules.home-manager 17 | clapfile.nixosModules.nixos 18 | agenix.nixosModules.default 19 | ../modules 20 | ]; 21 | 22 | home-manager = { 23 | sharedModules = [ ../../platforms/home-manager/modules ]; 24 | useGlobalPkgs = true; 25 | useUserPackages = true; 26 | }; 27 | }; 28 | }; 29 | 30 | makeTest = 31 | testModule: 32 | (pkgs.testers.runNixOSTest { 33 | imports = [ 34 | baseModule 35 | testModule 36 | ]; 37 | }) 38 | // { 39 | __test = true; 40 | }; 41 | 42 | importTests = path: args: callPackage path (args // { inherit importTests makeTest; }); 43 | in 44 | { 45 | dhcp = importTests ./dhcp.nix { }; 46 | dns = importTests ./dns.nix { }; 47 | filesystems = importTests ./filesystems { }; 48 | gateway = importTests ./gateway.nix { }; 49 | 50 | # A place to experiment locally. This is much faster than waiting for 51 | # a Colmena deploy. 52 | sandbox = importTests ./sandbox.nix { }; 53 | } 54 | -------------------------------------------------------------------------------- /platforms/nixos/tests/dhcp.nix: -------------------------------------------------------------------------------- 1 | { makeTest, lib, ... }: 2 | 3 | makeTest { 4 | name = "dhcp-assignment"; 5 | 6 | defaults.lab.networks.test.ipv4 = { 7 | cidr = "10.0.5.3/24"; 8 | dhcp.pools = [ 9 | { 10 | start = "10.0.5.22"; 11 | end = "10.0.5.22"; 12 | } 13 | ]; 14 | }; 15 | 16 | nodes = { 17 | server = 18 | { config, ... }: 19 | { 20 | virtualisation.vlans = [ 1 ]; 21 | networking.useDHCP = false; 22 | 23 | lab.services.dhcp = { 24 | enable = true; 25 | networks.test.interface = "eth1"; 26 | nameservers = [ config.lab.networks.test.ipv4.gateway ]; 27 | reservations = [ 28 | rec { 29 | type = "client-id"; 30 | ip-address = "10.0.5.68"; 31 | id = config.lab.services.dhcp.lib.toClientId ip-address; 32 | } 33 | ]; 34 | }; 35 | 36 | assertions = [ 37 | { 38 | assertion = lib.any (port: port == 67) config.networking.firewall.interfaces.eth1.allowedUDPPorts; 39 | 40 | message = '' 41 | DHCP server did not open firewall. 42 | ''; 43 | } 44 | ]; 45 | 46 | systemd.network = { 47 | enable = true; 48 | networks = { 49 | "01-eth1" = { 50 | name = "eth1"; 51 | networkConfig.Address = "10.0.5.11/24"; 52 | }; 53 | }; 54 | }; 55 | }; 56 | 57 | client = { 58 | virtualisation.vlans = [ 1 ]; 59 | systemd.services.systemd-networkd = { 60 | environment.SYSTEMD_LOG_LEVEL = "debug"; 61 | }; 62 | 63 | networking = { 64 | useNetworkd = true; 65 | useDHCP = false; 66 | interfaces.eth1.useDHCP = true; 67 | }; 68 | }; 69 | 70 | reserved = 71 | { config, ... }: 72 | { 73 | virtualisation.vlans = [ 1 ]; 74 | 75 | networking = { 76 | useDHCP = false; 77 | interfaces.eth1.useDHCP = true; 78 | 79 | dhcpcd.extraConfig = '' 80 | clientid ${config.lab.services.dhcp.lib.toClientId "10.0.5.68"} 81 | ''; 82 | }; 83 | }; 84 | }; 85 | 86 | testScript = '' 87 | import json 88 | 89 | server.start() 90 | client.start() 91 | 92 | server.wait_for_unit("kea-dhcp4-server.service") 93 | client.wait_for_unit("systemd-networkd-wait-online.service") 94 | 95 | with subtest("correct client IP is assigned"): 96 | client.wait_until_succeeds("ip addr show eth1 | grep -q '10.0.5.22/24'") 97 | 98 | with subtest("default gateway is assigned"): 99 | routes = json.loads(client.succeed("ip --json route")) 100 | gateways = { 101 | route["gateway"] for route in routes 102 | if ( 103 | route["dev"] == "eth1" and 104 | route["protocol"] == "dhcp" and 105 | "gateway" in route 106 | ) 107 | } 108 | 109 | assert "10.0.5.3" in gateways, f"Gateway was not assigned: {gateways}" 110 | 111 | with subtest("expected DNS servers are provided"): 112 | client.succeed("resolvectl dns eth1 | grep -q '10.0.5.3'") 113 | 114 | with subtest("reservations are given to recognized hosts"): 115 | reserved.start() 116 | reserved.wait_until_succeeds("ip addr show eth1 | grep -q '10.0.5.68/24'") 117 | ''; 118 | } 119 | -------------------------------------------------------------------------------- /platforms/nixos/tests/dns.nix: -------------------------------------------------------------------------------- 1 | { makeTest, ... }: 2 | 3 | makeTest { 4 | name = "dns-lookup"; 5 | 6 | nodes.machine = 7 | { 8 | config, 9 | pkgs, 10 | lib, 11 | ... 12 | }: 13 | { 14 | environment.systemPackages = [ 15 | config.services.etcd.package 16 | pkgs.dig 17 | pkgs.doggo 18 | ]; 19 | 20 | # TODO: Manage service discovery in a separate and focused test. 21 | lab.services.discovery.server = { 22 | enable = true; 23 | dns = { 24 | zone = "dyn.example.com"; 25 | prefix.name = "skydns"; 26 | }; 27 | }; 28 | 29 | lab.services.dns = { 30 | enable = true; 31 | interfaces = [ "eth1" ]; 32 | server.id = "magic-string-nsid"; 33 | 34 | discovery = { 35 | enable = true; 36 | zones = [ config.lab.services.discovery.server.dns.zone ]; 37 | dns.prefix = "/${config.lab.services.discovery.server.dns.prefix.name}"; 38 | }; 39 | 40 | zone = { 41 | name = "host.example.com"; 42 | records = [ 43 | { 44 | type = "TXT"; 45 | name = "custom-record"; 46 | value = "magic-string-record"; 47 | } 48 | ]; 49 | }; 50 | 51 | hosts.file = pkgs.writeText "hosts" '' 52 | 127.1.2.3 custom-host.arpa 53 | ''; 54 | 55 | # These are not used, but it is important to test the generation 56 | # code paths to validate that at least the server does not crash. 57 | forward = [ 58 | { 59 | zone = "non.existent.domain.one"; 60 | method = "udp"; 61 | udp.ip = "9.9.9.9"; 62 | } 63 | { 64 | zone = "non.existent.domain.two"; 65 | method = "resolv.conf"; 66 | } 67 | { 68 | zone = "."; 69 | method = "tls"; 70 | tls = { 71 | ip = "1.1.1.1"; 72 | servername = "cloudflare-dns.com"; 73 | }; 74 | } 75 | ]; 76 | }; 77 | 78 | assertions = [ 79 | { 80 | assertion = lib.any (port: port == 53) config.networking.firewall.interfaces.eth1.allowedUDPPorts; 81 | message = '' 82 | DNS server did not open firewall. 83 | ''; 84 | } 85 | ]; 86 | }; 87 | 88 | testScript = '' 89 | import json 90 | 91 | start_all() 92 | machine.wait_for_unit("coredns.service") 93 | machine.wait_for_unit("etcd.service") 94 | machine.wait_for_unit("network-online.target") 95 | 96 | with subtest("NSID is advertised in responses"): 97 | nsid_line = machine.succeed("dig +nsid @localhost localhost | grep NSID") 98 | print(nsid_line) 99 | assert "magic-string-nsid" in nsid_line, "NSID not in response" 100 | 101 | with subtest("resolves custom records"): 102 | result = machine.succeed("doggo @localhost TXT custom-record.host.example.com") 103 | print(result) 104 | assert "magic-string-record" in result, "Custom record not in response" 105 | 106 | with subtest("uses local server as system DNS resolver"): 107 | # Not specifying the server address - pull from `/etc/resolv.conf`. 108 | result = machine.succeed("doggo TXT custom-record.host.example.com") 109 | print(result) 110 | assert "magic-string-record" in result, "Local server was not used" 111 | 112 | with subtest("serves records from the host file"): 113 | result = machine.succeed("doggo custom-host.arpa") 114 | print(result) 115 | assert "127.1.2.3" in result, "Record from host file was not found" 116 | 117 | with subtest("resolves dynamic hosts from etcd"): 118 | payload = json.dumps({ "host": "10.20.30.40", "ttl": 3600, "type": "A" }) 119 | machine.succeed(f"etcdctl put /skydns/com/example/dyn/test '{payload}'") 120 | 121 | resolved = machine.succeed("doggo @localhost A test.dyn.example.com") 122 | print(resolved) 123 | assert "10.20.30.40" in resolved, "Dynamic record not in response" 124 | 125 | # No tests for DNS forwarding. Just try not to break it :) 126 | ''; 127 | } 128 | -------------------------------------------------------------------------------- /platforms/nixos/tests/filesystems/default.nix: -------------------------------------------------------------------------------- 1 | { importTests, ... }: 2 | 3 | { 4 | zfs = importTests ./zfs.nix { }; 5 | } 6 | -------------------------------------------------------------------------------- /platforms/nixos/tests/filesystems/zfs.nix: -------------------------------------------------------------------------------- 1 | { makeTest, ... }: 2 | 3 | # Measured in MiB. 4 | let 5 | disk-size = 2 * 1024; 6 | in 7 | 8 | makeTest { 9 | name = "zfs-management"; 10 | 11 | nodes.machine = 12 | { pkgs, lib, ... }: 13 | { 14 | virtualisation.emptyDiskImages = lib.replicate 4 disk-size; 15 | environment.systemPackages = [ pkgs.parted ]; 16 | networking.hostId = "00000000"; 17 | lab.filesystems.zfs = { 18 | enable = true; 19 | mounts = { 20 | "/mnt/pool" = "test-pool"; 21 | "/mnt/pool/dataset" = "test-pool/dataset"; 22 | }; 23 | 24 | pools = { 25 | plain = { 26 | vdevs = [ { sources = [ "vdb" ]; } ]; 27 | 28 | settings = { 29 | comment = "Test pool"; 30 | autotrim = "on"; 31 | }; 32 | 33 | properties.mountpoint = "none"; 34 | 35 | datasets = { 36 | test-1.properties.mountpoint = "none"; 37 | test-2.properties = { 38 | mountpoint = "none"; 39 | compression = "on"; 40 | }; 41 | }; 42 | }; 43 | 44 | fancy = { 45 | datasets.test.properties.mountpoint = "none"; 46 | vdevs = [ 47 | { 48 | type = "mirror"; 49 | sources = [ 50 | "vdc" 51 | "vdd" 52 | ]; 53 | } 54 | { 55 | type = "log"; 56 | sources = [ "vde" ]; 57 | } 58 | ]; 59 | }; 60 | }; 61 | }; 62 | }; 63 | 64 | testScript = '' 65 | import textwrap 66 | 67 | start_all() 68 | 69 | with subtest("pool creation"): 70 | machine.succeed("system fs init") 71 | 72 | # ZFS has no export format. The convention is parsing with awk. 73 | pool_details = machine.succeed( 74 | """ 75 | zpool status -vp | awk ' 76 | /pool:/ { pool = $2; vdev = "disk"; next } 77 | $1 ~ "logs" { vdev = $1; next } 78 | $1 ~ "mirror" { vdev = $1; next } 79 | $1 == pool { next } 80 | $1 == "NAME" { next } 81 | NF != 5 { next } 82 | $2 == "ONLINE" { print pool","vdev","$1 } 83 | ' | sort 84 | """ 85 | ) 86 | 87 | # NOTE: Parsing is fragile. Double check the pool interactively before 88 | # bothering to troubleshoot. 89 | 90 | assert pool_details.strip() == textwrap.dedent(""" 91 | fancy,logs,vde 92 | fancy,mirror-0,vdc 93 | fancy,mirror-0,vdd 94 | plain,disk,vdb 95 | """).strip(), "ZFS pools were not created correctly" 96 | 97 | with subtest("pool properties"): 98 | machine.succeed("zpool get comment plain | grep -q 'Test pool'") 99 | machine.succeed("zpool get autotrim plain | grep -q on") 100 | machine.succeed("zfs get mountpoint plain | grep -q none") 101 | 102 | with subtest("dataset creation"): 103 | datasets = machine.succeed( 104 | """ 105 | zfs list -pt filesystem | awk ' 106 | NR != 1 { print $1 } 107 | ' | sort 108 | """ 109 | ) 110 | 111 | assert datasets.strip() == textwrap.dedent(""" 112 | fancy 113 | fancy/test 114 | plain 115 | plain/test-1 116 | plain/test-2 117 | """).strip(), "ZFS datasets were not created correctly" 118 | 119 | with subtest("dataset properties"): 120 | machine.succeed("zfs get mountpoint plain/test-1 | grep -q none") 121 | machine.succeed("zfs get mountpoint plain/test-2 | grep -q none") 122 | machine.succeed("zfs get compression plain/test-2 | grep -q on") 123 | ''; 124 | } 125 | -------------------------------------------------------------------------------- /platforms/nixos/tests/gateway.nix: -------------------------------------------------------------------------------- 1 | { makeTest, ... }: 2 | 3 | let 4 | wan-vlan-id = 1; 5 | lan-vlan-id = 2; 6 | ip = { 7 | client = "10.60.5.10"; 8 | world = "192.168.65.0"; 9 | g8w_wan = "192.168.65.10"; 10 | g8w_lan = "10.60.5.0"; 11 | }; 12 | in 13 | 14 | makeTest { 15 | name = "gateway"; 16 | 17 | defaults.lab.networks = { 18 | lan.ipv4.cidr = "${ip.g8w_lan}/24"; 19 | wan.ipv4 = { 20 | cidr = "${ip.world}/24"; 21 | dhcp.pools = [ 22 | { 23 | start = ip.g8w_wan; 24 | end = ip.g8w_wan; 25 | } 26 | ]; 27 | }; 28 | }; 29 | 30 | nodes = { 31 | # `world` exists on the outer network segment. 32 | world = 33 | { config, ... }: 34 | 35 | let 36 | inherit (config.lab.networks) wan; 37 | in 38 | 39 | { 40 | services.httpd.enable = true; 41 | networking.firewall.allowedTCPPorts = [ 80 ]; 42 | virtualisation.vlans = [ wan-vlan-id ]; 43 | networking.interfaces.eth1.ipv4.addresses = [ 44 | { 45 | address = wan.ipv4.gateway; 46 | prefixLength = wan.ipv4.prefixLength; 47 | } 48 | ]; 49 | 50 | lab.services.dhcp = { 51 | enable = true; 52 | networks.wan.interface = "eth1"; 53 | }; 54 | }; 55 | 56 | # `gateway` bridges between the outer and inner network segments. 57 | gateway = 58 | { config, lib, ... }: 59 | { 60 | services.openssh.enable = true; 61 | virtualisation.vlans = [ 62 | wan-vlan-id 63 | lan-vlan-id 64 | ]; 65 | 66 | lab.services.gateway = { 67 | enable = true; 68 | wan.interface = "eth1"; 69 | networks.lan.interface = "eth2"; 70 | }; 71 | }; 72 | 73 | # `client` exists on the inner network segment. 74 | client = 75 | { config, ... }: 76 | { 77 | virtualisation.vlans = [ lan-vlan-id ]; 78 | services.httpd.enable = true; 79 | networking.firewall.allowedTCPPorts = [ 80 ]; 80 | 81 | networking = { 82 | defaultGateway = { 83 | address = config.lab.networks.lan.ipv4.gateway; 84 | interface = "eth1"; 85 | }; 86 | 87 | interfaces.eth1.ipv4.addresses = [ 88 | { 89 | address = ip.client; 90 | prefixLength = 24; 91 | } 92 | ]; 93 | }; 94 | }; 95 | }; 96 | 97 | testScript = '' 98 | import json 99 | 100 | world.start() 101 | client.start() 102 | 103 | world.wait_for_unit("network-online.target") 104 | world.wait_for_unit("httpd.service") 105 | 106 | client.wait_for_unit("network-online.target") 107 | client.wait_for_unit("httpd.service") 108 | 109 | # This makes sure there's no magic in the test that allows the client and 110 | # outside world to communicate directly. 111 | with subtest("client cannot reach world without gateway"): 112 | client.fail("curl --fail --connect-timeout 2 http://${ip.world}") 113 | 114 | gateway.start() 115 | gateway.wait_for_unit("network-online.target") 116 | gateway.wait_for_unit("sshd.service") 117 | 118 | with subtest("preflight checks"): 119 | world.succeed("curl --fail http://localhost") 120 | 121 | # Same network. If this fails, it implies a firewall issue on the "world". 122 | gateway.succeed("curl --fail http://${ip.world}") 123 | gateway.succeed("curl --fail http://${ip.client}") 124 | 125 | client.succeed("ping -c 1 ${ip.g8w_lan}") 126 | 127 | with subtest("gateway was assigned correct IP address"): 128 | result = json.loads(gateway.succeed("ip --json addr show eth1")) 129 | gateway_wan_ip = result[0]["addr_info"][1]["local"] 130 | 131 | print(f"Gateway WAN IP: {gateway_wan_ip}") 132 | assert gateway_wan_ip == "${ip.g8w_wan}", "Gateway has wrong WAN IP" 133 | 134 | with subtest("client can communicate through the gateway"): 135 | gateway.succeed("curl --fail http://${ip.world}") 136 | 137 | with subtest("SSH is not open to the world"): 138 | world.fail("nc -w 1 -z ${ip.g8w_wan} 22") 139 | 140 | with subtest("SSH is open on the LAN"): 141 | client.succeed("nc -w 1 -z ${ip.g8w_lan} 22") 142 | 143 | with subtest("outside world cannot reach the client"): 144 | # Requests from the outside should fail. 145 | world.fail("curl --fail --connect-timeout 2 http://${ip.client}") 146 | ''; 147 | } 148 | -------------------------------------------------------------------------------- /platforms/nixos/tests/sandbox.nix: -------------------------------------------------------------------------------- 1 | { makeTest, ... }: 2 | 3 | makeTest { 4 | name = "sandbox-environment"; 5 | 6 | nodes.machine = 7 | { pkgs, ... }: 8 | { 9 | environment.systemPackages = [ pkgs.hello ]; 10 | }; 11 | 12 | testScript = '' 13 | start_all() 14 | machine.shell_interact() 15 | ''; 16 | } 17 | -------------------------------------------------------------------------------- /secrets.nix: -------------------------------------------------------------------------------- 1 | let 2 | inherit (flake.inputs.nixpkgs) lib; 3 | flake = builtins.getFlake (toString ./.); 4 | 5 | /** 6 | Find all public keys for nodes that match the given predicate. 7 | */ 8 | getPublicKeysWhere = 9 | predicate: 10 | lib.pipe flake.outputs.colmenaHive.nodes [ 11 | (lib.filterAttrs (_: node: predicate node)) 12 | (lib.mapAttrsToList (_: node: node.config.lab.host.publicKeys)) 13 | (lib.flatten) 14 | ]; 15 | in 16 | 17 | { 18 | # Generated with: `cloudflared tunnel create ` 19 | "vpn-tunnel-key.age".publicKeys = getPublicKeysWhere ( 20 | node: node.config.lab.profiles.vpn.server.enable 21 | ); 22 | } 23 | --------------------------------------------------------------------------------