diff --git a/.gitignore b/.gitignore index d3930f2..e976b47 100644 --- a/.gitignore +++ b/.gitignore @@ -5,3 +5,4 @@ /Gemfile.lock /iso /vendor +/packer_cache diff --git a/Gemfile b/Gemfile deleted file mode 100644 index 980d66e..0000000 --- a/Gemfile +++ /dev/null @@ -1,17 +0,0 @@ -# -*- mode: ruby -*- -# vi: set ft=ruby : - -source :rubygems - -gem 'rake', '~> 10.0.2' - -group :chef do - gem 'chef', '~> 10.16.2' - gem 'foodcritic', '~> 1.6.1' - gem 'test-kitchen', '~> 0.6.0' -end - -group :vagrant do - gem 'vagrant', '~> 1.0.5' - gem 'veewee', '~> 0.3.1' -end diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..dcac0ac --- /dev/null +++ b/Makefile @@ -0,0 +1,17 @@ + +ISO_URL_32=http://nixos.org/releases/nixos/13.10/nixos-13.10.35455.45219b9/nixos-minimal-13.10.35455.45219b9-i686-linux.iso +ISO_MD5_32=c10f01734cb187e13068252e2cde565d +GUEST_OS_32=Linux + +ISO_URL_64=http://nixos.org/releases/nixos/13.10/nixos-13.10.35455.45219b9/nixos-minimal-13.10.35455.45219b9-x86_64-linux.iso +ISO_MD5_64=fbac8c11661028e6f9c4ec293e1329e1 +GUEST_OS_64=Linux_64 + +all: nixbox32-template.json nixbox64-template.json + +nixbox32-template.json: nixbox-template.json.orig + cat $< | sed -e 's|ISO_URL|$(ISO_URL_32)|g' -e 's|ISO_MD5|$(ISO_MD5_32)|g' -e 's|GUEST_OS|$(GUEST_OS_32)|g' -e 's|NIXBOX|nixbox32|g' > $@ + +nixbox64-template.json: nixbox-template.json.orig + cat $< | sed -e 's|ISO_URL|$(ISO_URL_64)|g' -e 's|ISO_MD5|$(ISO_MD5_64)|g' -e 's|GUEST_OS|$(GUEST_OS_64)|g' -e 's|NIXBOX|nixbox64|g' > $@ + diff --git a/README.md b/README.md new file mode 100644 index 0000000..2b588c2 --- /dev/null +++ b/README.md @@ -0,0 +1,34 @@ +NixOS boxes for Vagrant +======================= + +[NixOS](http://nixos.org) is a linux distribution based on a purely functional +package manager. This project builds .box images that can be used by +[vagrant](http://vagrantup.com), the VM manager. + +Status +------ + +stable + +Building the images +------------------- + +First install [packer](http://packer.io) and +[virtualbox](https://www.virtualbox.org/) + +Then: + +``` +packer build nixbox32-template.json +# or +packer build nixbox64-template.json +``` + +The .box image is now ready to go and you can use it in vagrant: + +``` +vagrant box add nixbox32 nixbox32-virtualbox.box +# or +vagrant box add nixbox64-nixbox64-virtualbox.box +``` + diff --git a/Vagrantfile b/Vagrantfile index 2a89fa3..d19f9a7 100644 --- a/Vagrantfile +++ b/Vagrantfile @@ -1,6 +1,7 @@ # -*- mode: ruby -*- # vi: set ft=ruby : -Vagrant::Config.run do |config| +Vagrant.configure("2") do |config| config.vm.box = 'nixos32' + config.vm.box_url = 'http://zimbatm.s3.amazonaws.com/nixbox/nixos32.box' end diff --git a/definitions/nixos32/definition.rb b/definitions/nixos32/definition.rb index fdaf3da..2021aec 100644 --- a/definitions/nixos32/definition.rb +++ b/definitions/nixos32/definition.rb @@ -1,6 +1,6 @@ iso = { - :file => 'nixos-minimal-0.2pre4454_979117b-789154b-i686-linux.iso', - :md5 => '0f3a1a5a531b01819ec17a0feeea11e6', + :url => 'http://nixos.org/releases/nixos/nixos-0.2pre4476_a5e4432-b076ab9/nixos-minimal-0.2pre4476_a5e4432-b076ab9-i686-linux.iso', + :md5 => 'f035bb375f0fbcbbc4e33a12131e91d4', } session = { @@ -27,9 +27,9 @@ session = { :disk_size => '20400', :hostiocache => 'off', :iso_download_timeout => '1000', - :iso_file => iso[:file], + :iso_file => File.basename(iso[:url]), :iso_md5 => iso[:md5], - :iso_src => "http://nixos.org/releases/nixos/#{iso[:file].gsub(/-[^-]+-linux.iso$/,'')}/#{iso[:file]}", + :iso_src => iso[:url], :kickstart_file => 'configuration.nix', :kickstart_port => '7122', :kickstart_timeout => '10000', diff --git a/definitions/nixos64/configuration.nix b/definitions/nixos64/configuration.nix new file mode 100644 index 0000000..31b1807 --- /dev/null +++ b/definitions/nixos64/configuration.nix @@ -0,0 +1,39 @@ +{ config, pkgs, ... }: +{ + require = [ ./hardware-configuration.nix ]; + boot = { + loader.grub.device = "/dev/sda"; + }; + environment.systemPackages = with pkgs; [ git ruby rubygems gnumake gcc ]; + fileSystems = [ { mountPoint = "/"; label = "nixos"; } ]; + security.sudo.configFile = + '' + Defaults:root,%wheel env_keep+=LOCALE_ARCHIVE + Defaults:root,%wheel env_keep+=NIX_PATH + Defaults:root,%wheel env_keep+=TERMINFO_DIRS + Defaults env_keep+=SSH_AUTH_SOCK + Defaults lecture = never + root ALL=(ALL) SETENV: ALL + %wheel ALL=(ALL) NOPASSWD: ALL, SETENV: ALL + ''; + services = { + dbus.enable = true; + openssh.enable = true; + }; + users = { + extraGroups = [ { name = "vagrant"; } { name = "vboxsf"; } ]; + extraUsers = [ { + description = "Vagrant User"; + name = "vagrant"; + group = "vagrant"; + extraGroups = [ "users" "vboxsf" "wheel" ]; + password = "vagrant"; + home = "/home/vagrant"; + createHome = true; + useDefaultShell = true; + openssh.authorizedKeys.keys = [ + "ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEA6NF8iallvQVp22WDkTkyrtvp9eWW6A8YVr+kz4TjGYe7gHzIw+niNltGEFHzD8+v1I2YJ6oXevct1YeS0o9HZyN1Q9qgCgzUFtdOKLv6IedplqoPkcmF0aYet2PkEDo3MlTBckFXPITAMzF8dJSIFo9D8HfdOV0IAdx4O7PtixWKn5y2hMNG0zQPyUecp4pzC6kivAIhyfHilFR61RGL+GPXQ2MWZWFYbAGjyiYJnAmCP3NOTd0jMZEnDkbUvxhMmBYSdETk1rRgm+R4LOzFUGaHqHDLKLX+FIPKcF96hrucXzcWyLbIbEgE98OHlnVYCzRdK8jlqm8tehUc9c9WhQ== vagrant insecure public key" + ]; + } ]; + }; +} diff --git a/definitions/nixos64/definition.rb b/definitions/nixos64/definition.rb new file mode 100644 index 0000000..233ced2 --- /dev/null +++ b/definitions/nixos64/definition.rb @@ -0,0 +1,50 @@ +iso = { + :url => 'http://nixos.org/releases/nixos/nixos-0.2pre4476_a5e4432-b076ab9/nixos-minimal-0.2pre4476_a5e4432-b076ab9-x86_64-linux.iso', + :md5 => '3e0a95f0b7634022e1b0d88c6fc5ab17', +} + +session = { + :boot_cmd_sequence => + [ + '', ''*15, + 'root', '', + 'stop sshd', '', # Make sure we don't accept connections until reboot + 'fdisk /dev/sda', '', + 'n', ''*5, + 'a', '', + '1', '', + 'w', '', + 'mkfs.ext4 -j -L nixos /dev/sda1', '', + 'mount LABEL=nixos /mnt', '', + 'nixos-option --install', '', + 'curl http://%IP%:%PORT%/configuration.nix > /mnt/etc/nixos/configuration.nix &&', '', + 'nixos-install &&', '', + 'reboot', '', + ], + :boot_wait => '5', + :cpu_count => '1', + :disk_format => 'VDI', + :disk_size => '20400', + :hostiocache => 'off', + :iso_download_timeout => '1000', + :iso_file => File.basename(iso[:url]), + :iso_md5 => iso[:md5], + :iso_src => iso[:url], + :kickstart_file => 'configuration.nix', + :kickstart_port => '7122', + :kickstart_timeout => '10000', + :memory_size => '256', + :os_type_id => 'Linux26_64', + :postinstall_files => [ 'postinstall.sh' ], + :postinstall_timeout => '10000', + :shutdown_cmd => 'shutdown -h now', + :ssh_guest_port => '22', + :ssh_host_port => '7222', + :ssh_key => '', + :ssh_login_timeout => '10000', + :ssh_password => 'vagrant', + :ssh_user => 'vagrant', + :sudo_cmd => "sudo '%f'" +} + +Veewee::Definition.declare session diff --git a/definitions/nixos64/postinstall.sh b/definitions/nixos64/postinstall.sh new file mode 100755 index 0000000..6e3e215 --- /dev/null +++ b/definitions/nixos64/postinstall.sh @@ -0,0 +1,28 @@ +#!/bin/sh + +# Remove VirtualBox Guest Additions ISO that the Veewee put in our +# home dir + +rm -f ~/*.iso + +# Install chef & puppet + +su - vagrant -c 'gem install chef puppet --user-install \ + --bindir=$HOME/bin --no-rdoc --no-ri' + +# Make sure we are totally up to date + +nixos-rebuild --upgrade switch + +# Cleanup any previous generations and delete old packages that can be +# pruned. + +for x in `seq 0 2` ; do + nix-env --delete-generations old + nix-collect-garbage -d +done + +# Zero out the disk (for better compression) + +dd if=/dev/zero of=/EMPTY bs=1M +rm -rf /EMPTY diff --git a/http/configuration.nix b/http/configuration.nix new file mode 100644 index 0000000..cd0ffb3 --- /dev/null +++ b/http/configuration.nix @@ -0,0 +1,63 @@ +{ config, pkgs, ... }: + +{ + imports = + [ # Include the results of the hardware scan. + ./hardware-configuration.nix + ]; + + # Use the GRUB 2 boot loader. + boot.loader.grub.enable = true; + boot.loader.grub.version = 2; + boot.loader.grub.device = "/dev/sda"; + + networking.hostName = "nixbox"; + + # Services to enable: + + # Enable the OpenSSH daemon. + services.openssh.enable = true; + + # Enable DBus + services.dbus.enable = true; + + # Default packages + environment.systemPackages = with pkgs; [ + gcc + git + gnumake + python + ruby + rubygems + ]; + + # Creates a "vagrant" users with password-less sudo access + users = { + extraGroups = [ { name = "vagrant"; } { name = "vboxsf"; } ]; + extraUsers = [ { + description = "Vagrant User"; + name = "vagrant"; + group = "vagrant"; + extraGroups = [ "users" "vboxsf" "wheel" ]; + password = "vagrant"; + home = "/home/vagrant"; + createHome = true; + useDefaultShell = true; + openssh.authorizedKeys.keys = [ + "ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEA6NF8iallvQVp22WDkTkyrtvp9eWW6A8YVr+kz4TjGYe7gHzIw+niNltGEFHzD8+v1I2YJ6oXevct1YeS0o9HZyN1Q9qgCgzUFtdOKLv6IedplqoPkcmF0aYet2PkEDo3MlTBckFXPITAMzF8dJSIFo9D8HfdOV0IAdx4O7PtixWKn5y2hMNG0zQPyUecp4pzC6kivAIhyfHilFR61RGL+GPXQ2MWZWFYbAGjyiYJnAmCP3NOTd0jMZEnDkbUvxhMmBYSdETk1rRgm+R4LOzFUGaHqHDLKLX+FIPKcF96hrucXzcWyLbIbEgE98OHlnVYCzRdK8jlqm8tehUc9c9WhQ== vagrant insecure public key" + ]; + } ]; + }; + + security.sudo.configFile = + '' + Defaults:root,%wheel env_keep+=LOCALE_ARCHIVE + Defaults:root,%wheel env_keep+=NIX_PATH + Defaults:root,%wheel env_keep+=TERMINFO_DIRS + Defaults env_keep+=SSH_AUTH_SOCK + Defaults lecture = never + root ALL=(ALL) SETENV: ALL + %wheel ALL=(ALL) NOPASSWD: ALL, SETENV: ALL + ''; + +} diff --git a/nixbox-template.json.orig b/nixbox-template.json.orig new file mode 100644 index 0000000..806f3b1 --- /dev/null +++ b/nixbox-template.json.orig @@ -0,0 +1,50 @@ +{ + "builders": [ + { + "type": "virtualbox", + "boot_wait": "1m", + "boot_command": [ + "root", + "stop sshd", + "fdisk /dev/sda", + "n", + "", + "a", + "1", + "w", + "", + "mkfs.ext4 -j -L nixos /dev/sda1", + "mount LABEL=nixos /mnt", + "nixos-generate-config --root /mnt", + "curl http://{{ .HTTPIP }}:{{ .HTTPPort }}/configuration.nix | sed -e 's|nixbox|NIXBOX|g' > /mnt/etc/nixos/configuration.nix", + "nixos-install && reboot" + ], + "guest_additions_mode": "disable", + "guest_os_type": "GUEST_OS", + "http_directory": "http", + "iso_checksum": "ISO_MD5", + "iso_checksum_type": "md5", + "iso_url": "ISO_URL", + "shutdown_command": "sudo shutdown -h now", + "ssh_password": "vagrant", + "ssh_port": 22, + "ssh_username": "vagrant", + "ssh_wait_timeout": "5m", + "virtualbox_version_file": ".vbox_version" + } + ], + "provisioners": [ + { + "type": "shell", + "execute_command": "sudo sh '{{.Path}}'", + "script": "scripts/postinstall.sh" + } + ], + "post-processors": [ + { + "type": "vagrant", + "output": "NIXBOX-{{.Provider}}.box" + } + ] +} + diff --git a/nixbox32-template.json b/nixbox32-template.json new file mode 100644 index 0000000..6cdaf2c --- /dev/null +++ b/nixbox32-template.json @@ -0,0 +1,50 @@ +{ + "builders": [ + { + "type": "virtualbox", + "boot_wait": "1m", + "boot_command": [ + "root", + "stop sshd", + "fdisk /dev/sda", + "n", + "", + "a", + "1", + "w", + "", + "mkfs.ext4 -j -L nixos /dev/sda1", + "mount LABEL=nixos /mnt", + "nixos-generate-config --root /mnt", + "curl http://{{ .HTTPIP }}:{{ .HTTPPort }}/configuration.nix | sed -e 's|nixbox|nixbox32|g' > /mnt/etc/nixos/configuration.nix", + "nixos-install && reboot" + ], + "guest_additions_mode": "disable", + "guest_os_type": "Linux", + "http_directory": "http", + "iso_checksum": "c10f01734cb187e13068252e2cde565d", + "iso_checksum_type": "md5", + "iso_url": "http://nixos.org/releases/nixos/13.10/nixos-13.10.35455.45219b9/nixos-minimal-13.10.35455.45219b9-i686-linux.iso", + "shutdown_command": "sudo shutdown -h now", + "ssh_password": "vagrant", + "ssh_port": 22, + "ssh_username": "vagrant", + "ssh_wait_timeout": "5m", + "virtualbox_version_file": ".vbox_version" + } + ], + "provisioners": [ + { + "type": "shell", + "execute_command": "sudo sh '{{.Path}}'", + "script": "scripts/postinstall.sh" + } + ], + "post-processors": [ + { + "type": "vagrant", + "output": "nixbox32-{{.Provider}}.box" + } + ] +} + diff --git a/nixbox64-template.json b/nixbox64-template.json new file mode 100644 index 0000000..fbd6e96 --- /dev/null +++ b/nixbox64-template.json @@ -0,0 +1,50 @@ +{ + "builders": [ + { + "type": "virtualbox", + "boot_wait": "1m", + "boot_command": [ + "root", + "stop sshd", + "fdisk /dev/sda", + "n", + "", + "a", + "1", + "w", + "", + "mkfs.ext4 -j -L nixos /dev/sda1", + "mount LABEL=nixos /mnt", + "nixos-generate-config --root /mnt", + "curl http://{{ .HTTPIP }}:{{ .HTTPPort }}/configuration.nix | sed -e 's|nixbox|nixbox64|g' > /mnt/etc/nixos/configuration.nix", + "nixos-install && reboot" + ], + "guest_additions_mode": "disable", + "guest_os_type": "Linux_64", + "http_directory": "http", + "iso_checksum": "fbac8c11661028e6f9c4ec293e1329e1", + "iso_checksum_type": "md5", + "iso_url": "http://nixos.org/releases/nixos/13.10/nixos-13.10.35455.45219b9/nixos-minimal-13.10.35455.45219b9-x86_64-linux.iso", + "shutdown_command": "sudo shutdown -h now", + "ssh_password": "vagrant", + "ssh_port": 22, + "ssh_username": "vagrant", + "ssh_wait_timeout": "5m", + "virtualbox_version_file": ".vbox_version" + } + ], + "provisioners": [ + { + "type": "shell", + "execute_command": "sudo sh '{{.Path}}'", + "script": "scripts/postinstall.sh" + } + ], + "post-processors": [ + { + "type": "vagrant", + "output": "nixbox64-{{.Provider}}.box" + } + ] +} + diff --git a/scripts/postinstall.sh b/scripts/postinstall.sh new file mode 100755 index 0000000..7942f9a --- /dev/null +++ b/scripts/postinstall.sh @@ -0,0 +1,19 @@ +#!/bin/sh + +# Make sure we are totally up to date + +nix-channel --add http://nixos.org/channels/nixos-13.10 nixos +nixos-rebuild switch --upgrade + +# Cleanup any previous generations and delete old packages that can be +# pruned. + +for x in `seq 0 2` ; do + nix-env --delete-generations old + nix-collect-garbage -d +done + +# Zero out the disk (for better compression) + +dd if=/dev/zero of=/EMPTY bs=1M +rm -rf /EMPTY