This repository has been archived by the owner on May 7, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
mmc-image.nix
executable file
·152 lines (130 loc) · 5.57 KB
/
mmc-image.nix
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#! /usr/bin/env nix-shell
#! nix-shell --pure --packages nixos-generators --run "nixos-generate -I nixpkgs=nix/nixpkgs -f sd-aarch64-installer --system aarch64-linux -c ./mmc-image.nix"
# This file contains a Nix expression that generates an aarch64-linux bootable DOS/MBR
# image when executed. The only dependency is nix-shell(1). The supported target platform
# is the Raspberry Pi 3 Model B. But other revisions and models may boot and even operate
# as expected.
#
# Upon boot the system will establish a reverse SSH proxy to your configured bastion as
# specifed in `config.nix` and start the required services needed for proper operation.
# For convenience, the Nix expression in `nix/ssh-bastion.nix` may be included in the system
# configuration that yields the host pointed to by `config.nix` to automatically setup all
# system-external services that are expected (Arrowhead, etc.)
# (TODO: rewrite the above paragarph)
# TODO: remove cruft we do not need: nixos-install, ZFS, etc.
{ pkgs, lib, config, ... }:
{
imports = [
<nixpkgs/nixos/modules/installer/cd-dvd/sd-image-aarch64.nix>
./nix/brickpi3.nix
];
sdImage.compressImage = true; # ./build.sh expects a zstd-compress image
# Latest release of major 5 doesn't always play ball with the hardware.
# Relase 4.19 is stable and "battle-tested".
# See <https://github.com/NixOS/nixpkgs/issues/82455>.
boot.kernelPackages = pkgs.linuxPackages_4_19;
networking.hostName = "ed7039e";
# Automatically connect to eduroam via wlan0 for remote access.
networking.wireless = {
enable = true;
interfaces = [ "wlan0" ];
networks = (import ./local-secrets.nix).networks;
};
systemd.services.wpa_supplicant.wantedBy = lib.mkForce [ "default.target" ];
# Ensure a correct system clock.
services.timesyncd.enable = true;
time.timeZone = "Europe/Stockholm";
# Enables us to inspect core dumps in a centralized manner (incl. timestamps)
systemd.coredump.enable = true;
# Automatically log in as root, require no passphrase.
users.users.root.initialHashedPassword = "";
services.mingetty.autologinUser = lib.mkForce "root";
# Automatically start SSH server after boot, and establish a reverse proxy
# with a known bastion. This allows us to access the system from any system
# with Internet access (and allows this system to live behind NAT:ed networks).
services.openssh = {
enable = true;
permitRootLogin = "yes";
};
systemd.services.sshd.wantedBy = lib.mkForce [ "multi-user.target" ];
environment.etc."id_rsa" = {
source = ./id_rsa;
user = "nixos";
mode = "0600";
};
users.extraUsers.root.openssh.authorizedKeys.keys = lib.attrValues (import ./nix/ssh-keys.nix);
systemd.services.ssh-port-forward = {
description = "forwarding reverse SSH connection to a known bastion";
wantedBy = [ "multi-user.target" ];
after = [ "network.target" "nss-lookup.target" ];
serviceConfig = let bastion = (import ./nix/config.nix).bastion; in {
ExecStart = ''
${pkgs.openssh}/bin/ssh -o StrictHostKeyChecking=no \
-TNR ${bastion.socketPath}:localhost:22 ${bastion.user}@${bastion.host} \
-i /etc/id_rsa
'';
StandardError = "journal";
Type = "simple";
# Upon exit, try to establish a new connection every 5s.
Restart = "always";
RestartSec = "5s";
StartLimitIntervalSec = "0";
};
};
# Image minification
# documentation.enable = lib.mkForce false;
# documentation.nixos.enable = lib.mkForce false;
environment.noXlibs = lib.mkForce true;
services.xserver.enable = false;
services.xserver.desktopManager.xterm.enable = lib.mkForce false;
services.udisks2.enable = lib.mkForce false;
security.polkit.enable = lib.mkForce false;
boot.supportedFilesystems = lib.mkForce [ "vfat" ];
i18n.supportedLocales = lib.mkForce [ (config.i18n.defaultLocale + "/UTF-8") ];
# Intall all nodes we have written (from under ./src/) and install
# all Python dependencies.
environment.systemPackages = with pkgs; let
derivations = pkgs.callPackage ./nix/derivations.nix { };
in [
screen # for decawave debugging
git # for convenience (see systemd.clone-robot-repo below)
# Required libs for Python nodes
(python3.buildEnv.override {
extraLibs = (with python3Packages; [
numpy # for motor controller
])
++ (lib.attrValues derivations.pythonLibs)
++ (builtins.attrValues (import ./nix/adafruit-blinka/requirements.nix { inherit pkgs; }).packages);
})
] ++ (with derivations.systemNodes; [
binaries
scripts
]);
# Describe the environment properly for the FT232H which we use for
# line-follwing.
environment.variables = {
BLINKA_FT232H = "1";
LD_LIBRARY_PATH = "${pkgs.libusb}/lib/";
};
# Clone this repo to the file system for convenience. While this
# could be done using some
#
# environment.etc."repo" = fetchgit { ... }
#
# that would symlink to the Nix store, and would be read-only.
systemd.services.clone-robot-repo = {
description = "Clone the robot's git repository";
serviceConfig.Type = "oneshot";
wantedBy = [ "multi-user.target" ];
after = [ "network.target" ];
# Try to clone until we succeed (in case Internet connection is spotty).
script = ''
mkdir -p /root/repo && cd /root/repo
while [ ! $(${pkgs.git}/bin/git rev-parse --is-inside-work-tree) ]; do
${pkgs.git}/bin/git clone https://github.com/tmplt/ed7039e.git . || true
done
'';
};
# Required for the LCM UDP multicast transport implementation
networking.firewall.allowedUDPPorts = [ 7667 ];
}