This page describes how to build and install arbitrary packages from source on a NixOS fleet using nixa.
I have a repo golang-samples that contains a file context_channel.go
. We will build and install this system-wide as golang-samples
.
Under nixa/modules
, i’ve created golang-samples.nix
:
{ pkgs, ... }: {
environment.systemPackages = with pkgs; [
(stdenv.mkDerivation {
name = "golang-samples";
src = fetchFromGitHub {
owner = "nihr43";
repo = "golang-samples";
rev = "cdf9462";
sha256 = "sha256-Z6Iot+xI1y85BQOC8ipDQEJUXKoilDBrrSBfJ7jybzI=";
};
nativeBuildInputs = [ go ];
buildPhase = ''
export GOCACHE=$TMPDIR/go-cache
mkdir -p $GOCACHE
go build -mod=vendor -o golang-samples context_channel.go
'';
installPhase = ''
mkdir -p $out/bin
cp golang-samples $out/bin/
'';
})
];
}
environment.systemPackages
is indeed allowed to be defined in multiple .nix
files; the lists will be concatinated.
In my inventory, we simply add this module to a host group:
lanner:
hosts:
10.0.0.1:
hostname: lanner-c8f0
stateversion: 24.11
loopback: 10.0.0.1
as_number: 65501
bgp_interfaces:
- enp2s0f0
- enp2s0f1
- enp2s0f2
- enp2s0f3
- enp6s0f0
- enp6s0f1
- enp8s0f0
- enp8s0f1
bridge_address: 172.30.191.1
modules:
- grub_sda_common.nix
- bgp.nix
- serial.nix
- golang-samples.nix
nix-channel: nixos-24.11
and after applying, the program is available:
nixa > nix-shell --run 'python3 nixa --limit lanner'
10.0.0.1 is reachable
applying modules ['grub_sda_common.nix', 'bgp.nix', 'serial.nix', 'golang-samples.nix'] to lanner: ['10.0.0.1']
10.0.0.1:
---
+++
@@ -8,5 +8,7 @@
./serial.nix
+ ./golang-samples.nix
+
];
}
rebuilding NixOS on 10.0.0.1
[root@lanner-c8f0:~]# golang-samples
tick
tock
tick
^C
Do that with ansible!