Skip to content

Commit

Permalink
feat: add Vagrant powered demo (#7)
Browse files Browse the repository at this point in the history
Signed-off-by: James Hillyerd <[email protected]>
  • Loading branch information
jhillyerd authored Sep 21, 2024
1 parent d609ece commit 033ff2e
Show file tree
Hide file tree
Showing 6 changed files with 163 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,5 @@ go.work
.direnv
debug.log
result
/examples/vagrant-demo/.vagrant
/labcoat
39 changes: 39 additions & 0 deletions examples/vagrant-demo/Vagrantfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# -*- mode: ruby -*-
# vi: set ft=ruby :

nodes = [
{ :name => "db-lab", :ip => "192.168.33.10" },
{ :name => "web-lab", :ip => "192.168.33.11" },
]

Vagrant.configure("2") do |config|
nodes.each do |node|
config.vm.define node[:name] do |nconf|
nconf.vm.box = "nixbox/nixos"
nconf.vm.box_version = "23.05" # 23.11 hangs; https://github.com/NixOS/nixpkgs/issues/262686
nconf.vm.box_check_update = false

nconf.vm.hostname = node[:name]
nconf.vm.network "private_network", type: "static", ip: node[:ip]
nconf.vm.synced_folder ".", "/vagrant", disabled: true

nconf.vm.provision "shell" do |s|
ssh_pub_key = File.readlines("#{ENV['HOME']}/.ssh/id_ed25519.pub").first.strip
s.inline = <<-SHELL
mkdir --mode=0700 /root/.ssh
echo #{ssh_pub_key} >> /root/.ssh/authorized_keys
SHELL
end

# Activate hostname, network config.
nconf.vm.provision "shell", inline: "nixos-rebuild boot 2>&1 | tee /tmp/build.log", reboot: true

nconf.vm.provider "libvirt" do |libvirt|
libvirt.driver = "kvm"
libvirt.cpus = 2
libvirt.cputopology :sockets => '1', :cores => '2', :threads => '1'
libvirt.memory = 1024 # nixos-rebuild silently fails with 512
end
end
end
end
27 changes: 27 additions & 0 deletions examples/vagrant-demo/flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

64 changes: 64 additions & 0 deletions examples/vagrant-demo/flake.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
{
description = "labcoat vagrant-demo system flake";

inputs = {
nixpkgs.url = "github:nixos/nixpkgs/nixos-24.05";
};

outputs = { nixpkgs, ... }:
let
inherit (nixpkgs) lib;
in
{
nixosConfigurations =
let
# Configuration shared by all systems.
commonModule = {
boot.loader.grub.enable = true;
boot.loader.grub.device = "/dev/vda";

boot.initrd.checkJournalingFS = false;

services.openssh = {
enable = true;
settings.PermitRootLogin = "yes";
};

system.stateVersion = "24.05";
};

# Creates a network module based on provided config.
networkModule = { hostName, ipAddr }: {
_module.args.deployAddr = ipAddr; # See labcoat deploy-host-attr config.
networking = {
inherit hostName;
interfaces = {
ens6.ipv4.addresses = [{
address = ipAddr;
prefixLength = 24;
}];
};
};
};
in
{
db-lab = lib.nixosSystem {
system = "x86_64-linux";
modules = [
commonModule
./hardware-configuration.nix
(networkModule { hostName = "db-lab"; ipAddr = "192.168.33.10"; })
];
};

web-lab = lib.nixosSystem {
system = "x86_64-linux";
modules = [
commonModule
./hardware-configuration.nix
(networkModule { hostName = "web-lab"; ipAddr = "192.168.33.11"; })
];
};
};
};
}
28 changes: 28 additions & 0 deletions examples/vagrant-demo/hardware-configuration.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Vagrant + kvm hardware config.
{ lib, modulesPath, ... }:
{
imports =
[ (modulesPath + "/profiles/qemu-guest.nix")
];

boot.initrd.availableKernelModules = [ "ata_piix" "virtio_pci" "virtio_scsi" "floppy" "sd_mod" "sr_mod" ];
boot.initrd.kernelModules = [ ];
boot.kernelModules = [ "kvm-amd" ];
boot.extraModulePackages = [ ];

fileSystems."/" =
{ device = "/dev/disk/by-uuid/f101d11d-cf4b-4705-96ee-bf21580ee386";
fsType = "ext4";
};

swapDevices = [ ];

# Enables DHCP on each ethernet and wireless interface. In case of scripted networking
# (the default) this is the recommended approach. When using systemd-networkd it's
# still possible to use this option, but it's recommended to use it in conjunction
# with explicit per-interface declarations with `networking.interfaces.<interface>.useDHCP`.
networking.useDHCP = lib.mkDefault true;
# networking.interfaces.ens4.useDHCP = lib.mkDefault true;

nixpkgs.hostPlatform = lib.mkDefault "x86_64-linux";
}
3 changes: 3 additions & 0 deletions examples/vagrant-demo/xdg-config/labcoat/config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Host deployment configuration. Nix attrs typically start with 'flake' or 'target'.
[hosts]
deploy-host-attr = 'target._module.args.deployAddr'

0 comments on commit 033ff2e

Please sign in to comment.