at 23.11-pre 3.2 kB view raw
1{ config, lib, pkgs, modules, ... }: 2 3with lib; 4 5let 6 7 # Location of the repository on the harddrive 8 nixosPath = toString ../..; 9 10 # Check if the path is from the NixOS repository 11 isNixOSFile = path: 12 let s = toString path; in 13 removePrefix nixosPath s != s; 14 15 # Copy modules given as extra configuration files. Unfortunately, we 16 # cannot serialized attribute set given in the list of modules (that's why 17 # you should use files). 18 moduleFiles = 19 # FIXME: use typeOf (Nix 1.6.1). 20 filter (x: !isAttrs x && !lib.isFunction x) modules; 21 22 # Partition module files because between NixOS and non-NixOS files. NixOS 23 # files may change if the repository is updated. 24 partitionedModuleFiles = 25 let p = partition isNixOSFile moduleFiles; in 26 { nixos = p.right; others = p.wrong; }; 27 28 # Path transformed to be valid on the installation device. Thus the 29 # device configuration could be rebuild. 30 relocatedModuleFiles = 31 let 32 relocateNixOS = path: 33 "<nixpkgs/nixos" + removePrefix nixosPath (toString path) + ">"; 34 in 35 { nixos = map relocateNixOS partitionedModuleFiles.nixos; 36 others = []; # TODO: copy the modules to the install-device repository. 37 }; 38 39 # A dummy /etc/nixos/configuration.nix in the booted CD that 40 # rebuilds the CD's configuration (and allows the configuration to 41 # be modified, of course, providing a true live CD). Problem is 42 # that we don't really know how the CD was built - the Nix 43 # expression language doesn't allow us to query the expression being 44 # evaluated. So we'll just hope for the best. 45 configClone = pkgs.writeText "configuration.nix" 46 '' 47 { config, pkgs, ... }: 48 49 { 50 imports = [ ${toString config.installer.cloneConfigIncludes} ]; 51 52 ${config.installer.cloneConfigExtra} 53 } 54 ''; 55 56in 57 58{ 59 60 options = { 61 62 installer.cloneConfig = mkOption { 63 default = true; 64 description = lib.mdDoc '' 65 Try to clone the installation-device configuration by re-using it's 66 profile from the list of imported modules. 67 ''; 68 }; 69 70 installer.cloneConfigIncludes = mkOption { 71 default = []; 72 example = [ "./nixos/modules/hardware/network/rt73.nix" ]; 73 description = lib.mdDoc '' 74 List of modules used to re-build this installation device profile. 75 ''; 76 }; 77 78 installer.cloneConfigExtra = mkOption { 79 default = ""; 80 description = lib.mdDoc '' 81 Extra text to include in the cloned configuration.nix included in this 82 installer. 83 ''; 84 }; 85 }; 86 87 config = { 88 89 installer.cloneConfigIncludes = 90 relocatedModuleFiles.nixos ++ relocatedModuleFiles.others; 91 92 boot.postBootCommands = 93 '' 94 # Provide a mount point for nixos-install. 95 mkdir -p /mnt 96 97 ${optionalString config.installer.cloneConfig '' 98 # Provide a configuration for the CD/DVD itself, to allow users 99 # to run nixos-rebuild to change the configuration of the 100 # running system on the CD/DVD. 101 if ! [ -e /etc/nixos/configuration.nix ]; then 102 cp ${configClone} /etc/nixos/configuration.nix 103 fi 104 ''} 105 ''; 106 107 }; 108 109}