git.iko.soy/typeable/nixpkgs
on master help sign in
nixos/vagrant-virtualbox-image: init (#101120)
a2ee5cbJoseph D. Long
nixos/modules/virtualisation/vagrant-guest.nixnew file+58
@@ -0,0 +1,58 @@
1+# Minimal configuration that vagrant depends on
2+
3+{ config, pkgs, ... }:
4+let
5+ # Vagrant uses an insecure shared private key by default, but we
6+ # don't use the authorizedKeys attribute under users because it should be
7+ # removed on first boot and replaced with a random one. This script sets
8+ # the correct permissions and installs the temporary key if no
9+ # ~/.ssh/authorized_keys exists.
10+ install-vagrant-ssh-key = pkgs.writeScriptBin "install-vagrant-ssh-key" ''
11+ #!${pkgs.runtimeShell}
12+ if [ ! -e ~/.ssh/authorized_keys ]; then
13+ mkdir -m 0700 -p ~/.ssh
14+ echo "ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEA6NF8iallvQVp22WDkTkyrtvp9eWW6A8YVr+kz4TjGYe7gHzIw+niNltGEFHzD8+v1I2YJ6oXevct1YeS0o9HZyN1Q9qgCgzUFtdOKLv6IedplqoPkcmF0aYet2PkEDo3MlTBckFXPITAMzF8dJSIFo9D8HfdOV0IAdx4O7PtixWKn5y2hMNG0zQPyUecp4pzC6kivAIhyfHilFR61RGL+GPXQ2MWZWFYbAGjyiYJnAmCP3NOTd0jMZEnDkbUvxhMmBYSdETk1rRgm+R4LOzFUGaHqHDLKLX+FIPKcF96hrucXzcWyLbIbEgE98OHlnVYCzRdK8jlqm8tehUc9c9WhQ== vagrant insecure public key" >> ~/.ssh/authorized_keys
15+ chmod 0600 ~/.ssh/authorized_keys
16+ fi
17+ '';
18+in
19+{
20+ # Enable the OpenSSH daemon.
21+ services.openssh.enable = true;
22+
23+ # Packages used by Vagrant
24+ environment.systemPackages = with pkgs; [
25+ findutils
26+ iputils
27+ nettools
28+ netcat
29+ nfs-utils
30+ rsync
31+ ];
32+
33+ users.extraUsers.vagrant = {
34+ isNormalUser = true;
35+ createHome = true;
36+ description = "Vagrant user account";
37+ extraGroups = [ "users" "wheel" ];
38+ home = "/home/vagrant";
39+ password = "vagrant";
40+ useDefaultShell = true;
41+ uid = 1000;
42+ };
43+
44+ systemd.services.install-vagrant-ssh-key = {
45+ description = "Vagrant SSH key install (if needed)";
46+ after = [ "fs.target" ];
47+ wants = [ "fs.target" ];
48+ wantedBy = [ "multi-user.target" ];
49+ serviceConfig = {
50+ ExecStart = "${install-vagrant-ssh-key}/bin/install-vagrant-ssh-key";
51+ User = "vagrant";
52+ # So it won't be (needlessly) restarted:
53+ RemainAfterExit = true;
54+ };
55+ };
56+
57+ security.sudo.wheelNeedsPassword = false;
58+}
nixos/modules/virtualisation/vagrant-virtualbox-image.nixnew file+60
@@ -0,0 +1,60 @@
1+# Vagrant + VirtualBox
2+
3+{ config, pkgs, ... }:
4+
5+{
6+ imports = [
7+ ./vagrant-guest.nix
8+ ./virtualbox-image.nix
9+ ];
10+
11+ virtualbox.params = {
12+ audio = "none";
13+ audioin = "off";
14+ audioout = "off";
15+ usb = "off";
16+ usbehci = "off";
17+ };
18+ sound.enable = false;
19+ documentation.man.enable = false;
20+ documentation.nixos.enable = false;
21+
22+ users.extraUsers.vagrant.extraGroups = [ "vboxsf" ];
23+
24+ # generate the box v1 format which is much easier to generate
25+ # https://www.vagrantup.com/docs/boxes/format.html
26+ system.build.vagrantVirtualbox = pkgs.runCommand
27+ "virtualbox-vagrant.box"
28+ {}
29+ ''
30+ mkdir workdir
31+ cd workdir
32+
33+ # 1. create that metadata.json file
34+ echo '{"provider":"virtualbox"}' > metadata.json
35+
36+ # 2. create a default Vagrantfile config
37+ cat <<VAGRANTFILE > Vagrantfile
38+ Vagrant.configure("2") do |config|
39+ config.vm.base_mac = "0800275F0936"
40+ end
41+ VAGRANTFILE
42+
43+ # 3. add the exported VM files
44+ tar xvf ${config.system.build.virtualBoxOVA}/*.ova
45+
46+ # 4. move the ovf to the fixed location
47+ mv *.ovf box.ovf
48+
49+ # 5. generate OVF manifest file
50+ rm *.mf
51+ touch box.mf
52+ for fname in *; do
53+ checksum=$(sha256sum $fname | cut -d' ' -f 1)
54+ echo "SHA256($fname)= $checksum" >> box.mf
55+ done
56+
57+ # 6. compress everything back together
58+ tar --owner=0 --group=0 --sort=name --numeric-owner -czf $out .
59+ '';
60+}