Skip to content

Commit 6041f78

Browse files
committed
hosts/bastille: automatically set hostname based on mac address
1 parent 3a8573f commit 6041f78

2 files changed

Lines changed: 40 additions & 0 deletions

File tree

hosts/bastille/auto-hostname.nix

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
{ pkgs, lib, ... }:
2+
let
3+
# TODO: make this like a python script with a list of interfaces in order of preference
4+
auto-hostname = pkgs.writeShellApplication {
5+
name = "auto-hostname";
6+
7+
runtimeInputs = [
8+
pkgs.hostname
9+
];
10+
11+
text = ''
12+
if [[ -e "/sys/class/net/eno2/address" ]]; then
13+
mac_file="/sys/class/net/eno2/address"
14+
else
15+
mac_file=/sys/class/net/enp0s25/address
16+
fi
17+
18+
mac=$(cat $mac_file | tr -d '\r\n ' | tr ':' '-')
19+
20+
hostname "blade-$mac"
21+
'';
22+
};
23+
in {
24+
networking.hostName = "";
25+
26+
systemd.services."auto-hostname" = {
27+
wantedBy = [ "multi-user.target" ];
28+
after = [ "network.target" ];
29+
30+
unitConfig = {
31+
Description = "Automatically set the hostname ";
32+
};
33+
34+
serviceConfig = {
35+
Type = "oneshot";
36+
ExecStart = "${lib.getExe auto-hostname}";
37+
};
38+
};
39+
}

hosts/bastille/blade.nix

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{ modulesPath, pkgs, lib, ... }: {
22
imports = [
3+
./auto-hostname.nix
34
(import ../common/k3s.nix {})
45
../common/nix.nix
56
../common/sshd.nix

0 commit comments

Comments
 (0)