-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmanx.sh
More file actions
executable file
·4454 lines (4250 loc) · 160 KB
/
manx.sh
File metadata and controls
executable file
·4454 lines (4250 loc) · 160 KB
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!env bash
# Name: manx (Make Automated NixOS)
# Version: 1.8.1
# Release: 1
# License: CC-BA (Creative Commons By Attribution)
# http://creativecommons.org/licenses/by/4.0/legalcode
# Group: System
# Source: N/A
# URL: https://github.com/lateralblast/manx
# Distribution: NixOS
# Vendor: Linux
# Packager: Richard Spindler <richard@lateralblast.com.au>
# Description: A template for writing shell scripts
# Insert some shellcheck disables
# Depending on your requirements, you may want to add/remove disables
# shellcheck disable=SC2004
# shellcheck disable=SC2012
# shellcheck disable=SC2089
# shellcheck disable=SC2034
# shellcheck disable=SC1090
# shellcheck disable=SC2129
# shellcheck disable=SC2199
# shellcheck disable=SC2239
# Create arrays
declare -A os
declare -A vm
declare -A script
declare -A options
declare -a options_list
declare -a actions_list
# Grab script information and put it into an associative array
script['args']="$*"
script['file']="$0"
script['name']="manx"
script['file']=$( realpath "${script['file']}" )
script['path']=$( dirname "${script['file']}" )
script['modulepath']="${script['path']}/modules"
script['bin']=$( basename "${script['file']}" )
script['user']=$( id -u -n )
# Function: set_defaults
#
# Set defaults
set_defaults () {
# System kernel parameters - Security related
options['audit']="true" # option : Auditd parameters
options['auditrules']="" # option : Auditd parameters
options['kernelparams']="" # option : Additional kernel parameters to add to system grub commands
# Root filesystem subvolumes
options['subvolumes']="" # option : Root filesystem subvolumes
subvolumes=(
"root"
"var"
"home"
"nix"
"usr"
)
options['subvolumes']="${subvolumes[*]}"
# ZFS options
options['compression']="" # option : Root FS Compression type
options['zfsoptions']="" # option : ZFS options
zfsoptions=(
"-O mountpoint=none"
"-O atime=off"
"-O compression=lz4"
"-O xattr=sa"
"-O acltype=posixacl"
"-o ashift=12"
)
options['zfsoptions']="${zfsoptions[*]}"
options['btrfsoptions']="" # option : BTRFS options
btrfsoptions=(
"rw"
"relatime"
"compress=zstd:3"
"discard=async"
"space_cache=v2"
)
options['btrfsoptions']="${btrfsoptions[*]}"
options['isosystempackages']="" # option : ISO system packages
isosystempackages=(
"aide"
"ansible"
"btop"
"curl"
"dmidecode"
"efibootmgr"
"ethtool"
"file"
"fwupd"
"git"
"kernel-hardening-checker"
"lsb-release"
"lsof"
"lshw"
"lynis"
"nixos-generators"
"nmap"
"pciutils"
"ripgrep"
"rclone"
"tmux"
"usbutils"
"vim"
"wget"
)
options['isosystempackages']="${isosystempackages[*]}"
options['isostorepackages']="" # option : ISO store packages
isostorepackages=(
"aide"
"ansible"
"btop"
"curl"
"dmidecode"
"efibootmgr"
"ethtool"
"file"
"fwupd"
"git"
"kernel-hardening-checker"
"lsb-release"
"lsof"
"lshw"
"lynis"
"nixos-generators"
"nmap"
"pciutils"
"ripgrep"
"rclone"
"tmux"
"usbutils"
"vim"
"wget"
)
options['isostorepackages']="${isostorepackages[*]}"
options['systempackages']="" # option : System packages
systempackages=(
"aide"
"ansible"
"btop"
"curl"
"dmidecode"
"efibootmgr"
"ethtool"
"file"
"fwupd"
"git"
"kernel-hardening-checker"
"lsb-release"
"lsof"
"lshw"
"lynis"
"nixos-generators"
"nmap"
"pciutils"
"ripgrep"
"rclone"
"tmux"
"usbutils"
"vim"
"wget"
)
options['systempackages']="${systempackages[*]}"
# Blacklist
options['blacklist']="" # option : Blacklisted kernel modules
blacklist=(
"dccp"
"sctp"
"rds"
"tipc"
"n-hdlc"
"ax25"
"netrom"
"x25"
"rose"
"decnet"
"econet"
"af_802154"
"ipx"
"appletalk"
"psnap"
"p8023"
"p8022"
"can"
"atm"
"cramfs"
"freevxfs"
"jffs2"
"hfs"
"hfsplus"
"udf"
)
options['blacklist']="${blacklist[*]}"
# Available kernel modules
options['availmods']="" # option : Available kernel modules
availmods=(
"ahci"
"ehci_pci"
"megaraid_sas"
"sdhci_pci"
"sd_mod"
"sr_mod"
"usbhid"
"usb_storage"
"virtio_blk"
"virtio_pci"
"xhci_pci"
)
options['availmods']="${availmods[*]}"
# Serial parameters
options['serialspeed']="115200" # option : Serial speed
options['serialunit']="0" # option : Serial unit
options['serialword']="8" # option : Serial word
options['serialparity']="no" # option : Serial parity
options['serialstop']="1" # option : Serial stop
options['serialport']="0x02f8" # option : Serial port
options['serialtty']="ttyS0" # option : Serial TTY
# Kernel parameters
options['isokernelparams']="" # option : Additional kernel parameters to add to ISO grub commands
options['kernelparams']="" # option : Additional kernel parameters to add to system grub commands
options['serialkernelparams']="" # option : Serial kernel params
options['serialextraconfig']="" # option : Serial extra args
# Imports
options['isoimports']="" # option : ISO imports
isoimports=(
"<nixpkgs/nixos/modules/installer/cd-dvd/installation-cd-minimal-combined.nix>"
"<nixpkgs/nixos/modules/installer/cd-dvd/channel.nix>"
"<nixpkgs/nixos/modules/system/boot/loader/grub/grub.nix>"
"<nixpkgs/nixos/modules/system/boot/kernel.nix>"
)
options['isoimports']="${isoimports[*]}"
options['imports']="" # option : System imports
imports=(
"<nixpkgs/nixos/modules/system/boot/loader/grub/grub.nix>"
"<nixpkgs/nixos/modules/system/boot/kernel.nix>"
)
options['imports']="${imports[*]}"
# SSH Kex Algorithms
options['kexalgorithms']="" # option : SSH Key Exchange Algorithms
kexalgorithms=(
"curve25519-sha256@libssh.org"
"ecdh-sha2-nistp521"
"ecdh-sha2-nistp384"
"ecdh-sha2-nistp256"
"diffie-hellman-group-exchange-sha256"
)
options['kexalgorithms']="${kexalgorithms[*]}"
# SSH ciphers
options['ciphers']="" # option : SSH Ciphers
ciphers=(
"chacha20-poly1305@openssh.com"
"aes256-gcm@openssh.com"
"aes128-gcm@openssh.com"
"aes256-ctr"
"aes192-ctr"
"aes128-ctr"
)
options['ciphers']="${ciphers[*]}"
# SSH Macs
options['macs']="" # option : SSH Macs
macs=(
"hmac-sha2-512-etm@openssh.com"
"hmac-sha2-256-etm@openssh.com"
"umac-128-etm@openssh.com"
"hmac-sha2-512"
"hmac-sha2-256"
"umac-128@openssh.com"
)
options['macs']="${macs[*]}"
# fail2pan ignore IPs
options['ignoreip']="" # option : fail2ban ignore ip
ignoreip=(
"172.16.0.0/12"
"192.168.0.0/16"
)
options['ignoreip']="${ignoreip[*]}"
# Journald extra config
options['journaldextraconfig']="" # option : Journald extra config
journaldextraconfig=(
"SystemMaxUse=500M"
"SystemMaxFileSize=50M"
)
options['journaldextraconfig']="${journaldextraconfig[*]}"
options['journaldupload']="false" # option : Journald remote log upload
# Options
options['fwupd']="true" # option : Enable fwupd
options['secure']="true" # option : Enable secure parameters
options['sysctl']="" # option : System sysctl parameters
options['prefix']="ai" # option : Install directory prefix
options['verbose']="false" # option : Verbose mode
options['testmode']="false" # option : Test mode
options['strict']="false" # option : Strict mode
options['dryrun']="false" # option : Dryrun mode
options['debug']="false" # option : Debug mode
options['force']="false" # option : Force actions
options['mask']="false" # option : Mask identifiers
options['yes']="false" # option : Answer yes to questions
options['dhcp']="true" # option : DHCP network
options['swap']="true" # option : Use swap
options['lvm']="false" # option : Use LVM
options['zsh']="true" # option : Enable zsh
options['preserve']="false" # option : Preserve ISO
options['workdir']="${HOME}/${script['name']}" # option : Script work directory
options['sshkey']="" # option : SSH key
options['rootdisk']="first" # option : Disk
options['nic']="first" # option : NIC
options['zfs']="true" # option : ZFS filesystem
options['ext4']="false" # option : EXT4 filesystem
options['locale']="en_AU.UTF-8" # option : Locale
options['timezone']="Australia/Melbourne" # option : Timezone
options['username']="nixos" # option : Username
options['installuser']="nixos" # option : Install username
options['userpassword']="nixos" # option : User Password
options['usercrypt']="" # option : User Password Crypt
options['hostname']="nixos" # option : Hostname
options['domainname']="" # option : Domainname
options['sshkeyfile']="" # option : SSH key file
options['bootfs']="vfat" # option : Boot filesystem
options['rootfs']="zfs" # option : Root filesystem
options['firmware']="bios" # option : Boot firmware type
options['bios']="true" # option : BIOS Boot firmware
options['uefi']="false" # option : UEFI Boot firmware
options['isomount']="/iso" # option : ISO mount directory
options['oneshotscript']="${options['workdir']}/${options['prefix']}/oneshot.sh" # option : Oneshot script
options['installscript']="${options['workdir']}/${options['prefix']}/install.sh" # option : Install script
options['nixisoconfig']="${options['workdir']}/iso.nix" # option : NixOS ISO config
options['runsize']="50%" # option : Run size
options['source']="${options['workdir']}/${options['prefix']}" # option : Source directory for ISO additions
options['target']="/${options['prefix']}" # option : Target directory for ISO additions
options['installdir']="/mnt" # option : Install directory
options['nixdir']="${options['installdir']}/etc/nixos" # option : NixOS directory for configs
options['nixconfig']="${options['nixdir']}/configuration.nix" # option : NixOS install config file
options['nixhwconfig']="${options['nixdir']}/hardware-configuration.nix" # option : NixOS install hardware config file
options['nixzfsconfig']="${options['nixdir']}/zfs.nix" # option : NixOS install ZFS config file
options['systemd-boot']="true" # option : systemd-boot
options['touchefi']="true" # option : Touch EFI
options['sshserver']="true" # option : Enable SSH server
options['swapsize']="2G" # option : Swap partition size
options['rootsize']="100%FREE" # option : Root partition size
options['rootpool']="rpool" # option : Root pool name
options['rootpassword']="nixos" # option : Root password
options['rootcrypt']="" # option : Root password crypt
options['username']="nixos" # option : User Username
options['usergecos']="Admin" # option : User GECOS
options['usershell']="zsh" # option : User Shell
options['normaluser']="true" # option : Normal User
options['extragroups']="wheel" # option : Extra Groups
options['sudousers']="${options['username']}" # option : Sudo Users
options['sudocommand']="ALL" # option : Sudo Command
options['sudooptions']="NOPASSWD" # option : Sudo Options
options['experimental-features']="nix-command flakes" # option : Experimental Features
options['unfree']="false" # option : Allow Non Free Packages
options['stateversion']="25.05" # option : State version
options['latestversion']="25.11" # option : State version (latest)
options['unattended']="true" # option : Execute install script
options['attended']="false" # option : Don't execute install script
options['reboot']="true" # option : Reboot after install
options['poweroff']="true" # option : Poweroff after install
options['nixinstall']="true" # option : Run Nix installer on ISO
options['gfxmode']="auto" # option : Grub graphics mode
options['gfxpayload']="text" # option : Grub graphics payload
options['networkmanager']="true" # option : Enable NetworkManager
options['xserver']="false" # option : Enable Xserver
options['keymap']="au" # option : Keymap
options['videodriver']="" # option : Video Driver
options['sddm']="false" # option : KDE Plasma Login Manager
options['plasma6']="false" # option : KDE Plasma
options['gdm']="false" # option : Gnome Login Manager
options['gnome']="false" # option : Gnome
options['rootkit']="false" # option : Enable rootkit protection
options['bridge']="false" # option : Enable bridge
options['bridgenic']="br0" # option : Bridge NIC
options['ip']="" # option : IP Address
options['cidr']="24" # option : CIDR
options['dns']="8.8.8.8" # option : DNS/Nameserver address
options['gateway']="" # option : Gateway address
options['standalone']="false" # option : Package all requirements on ISO
options['rootvolname']="nixos" # option : Root volume name
options['bootvolname']="boot" # option : Boot volume name
options['mbrvolname']="bootcode" # option : Boot volume name
options['swapvolname']="swap" # option : Swap volume name
options['uefivolname']="uefi" # option : UEFI volume name
options['homevolname']="home" # option : Home volume name
options['nixvolname']="nix" # option : Nix volume name
options['usrvolname']="usr" # option : Usr volume name
options['varvolname']="var" # option : Var volume name
options['tempdir']="/tmp" # option : Temp directory
options['mbrpart']="1" # option : MBR partition
options['rootpart']="2" # option : Root partition
options['efipart']="3" # option : UEFI/Boot partition
options['swappart']="4" # option : Swap partition
options['devnodes']="/dev/disk/by-uuid" # option : Device nodesDevice nodes
options['logdir']="/var/log" # option : Install log dir
options['logfile']="${options['logdir']}/install.log" # option : Install log file
options['bootsize']="512M" # option : Boot partition size
options['isogrubextraconfig']="" # option : Additional kernel config to add to ISO grub commands
options['grubextraconfig']="" # option : Additional kernel config to add to system grub commands
options['initmods']='' # option : Available system init modules
options['bootmods']='' # option : Available system boot modules
options['oneshot']="true" # option : Enable oneshot service
options['serial']="true" # option : Enable serial
options['kernel']="" # option : Kernel
options['passwordauthentication']="false" # option : SSH Password Authentication
options['permitemptypasswords']="false" # option : SSH permit empty passwords
options['permittunnel']="false" # option : SSH permit tunnel
options['usedns']="false" # option : SSH use DNS
options['nixosgenerate']="false" # option : Use NixOS generate
options['kbdinteractive']="false" # option : SSH allow interactive kerboard authentication
options['x11forwarding']="false" # option : SSH allow X11 forwarding
options['maxauthtries']="3" # option : SSH max auth tries
options['maxsessions']="2" # option : SSH max sessions
options['clientaliveinterval']="300" # option : SSH client alive interval
options['clientalivecountmax']="0" # option : SSH client alive max count
options['firewall']="true" # option : Enable firewall
options['allowedtcpports']="22" # option : Allowed TCP ports
options['allowedudpports']="" # option : Allowed UDP ports
options['allowusers']="${options['username']}" # option : SSH allow user
options['allowtcpforwarding']="false" # option : SSH allow TCP forwarding
options['allowagentforwarding']="false" # option : SSH allow agent forwarding
options['permitrootlogin']="no" # option : SSH permit root login
options['isopermitrootlogin']="no" # option : SSH permit root login for install
options['loglevel']="VERBOSE" # option : SSH log level
options['hostkeystype']="ed25519" # option : SSH hosts keys type
options['hostkeyspath']="/etc/ssh/ssh_host_${options['hostkeystype']}_key" # option : SSH hosts key type
options['import']="" # option : Import Nix config to add to system build
options['isoimport']="" # option : Import Nix config to add to ISO build
options['dockerarch']=$( uname -m |sed 's/x86_/amd/g' ) # option : Docker architecture
options['targetarch']=$( uname -m ) # option : Target architecture
options['createdockeriso']="false" # option : Create ISO using docker
options['console']="false" # option : Enable console in actions
options['suffix']="" # option : Output file suffix
options['fail2ban']="true" # option : Enable fail2ban
options['maxretry']="5" # option : fail2ban max retry
options['bantime']="1h" # option : fail2ban ban time
options['bantimeincrement']="true" # option : fail2ban ban time increment
options['multipliers']="1 2 4 8 16 32 64 128 256" # option : fail2ban ban time multipliers
options['maxtime']="1h" # option : fail2ban max time
options['overalljails']="true" # option : Enable fail2ban overalljails
options['protectkernelimage']="true" # option : Protect kernel image
options['lockkernelmodules']="false" # option : Lock kernel modules
options['allowusernamespaces']="true" # option : Allow user name spaces
options['forcepagetableisolation']="true" # option : Force page table isolation
options['unprivilegedusernsclone']="config.virtualisation.containers.enable" # option : Disable unprivileged user namespaces
options['allowsmt']="true" # option : Allow SMT
options['dbusimplementation']="broker" # option : Dbus implementation
options['execwheelonly']="true" # option : Sudo exec wheel only
options['systemdumask']="0077" # option : systemd umask
options['privatenetwork']="true" # option : systemd private network
options['protecthostname']="true" # option : systemd protect hostname
options['protectkernelmodules']="true" # option : systemd protect kernel modules
options['protectsystem']="strict" # option : systemd protect system
options['protecthome']="true" # option : systemd protect home
options['protectkerneltunables']="true" # option : systemd protect kernel tunables
options['protectcontrolgroups']="true" # option : systemd protect control groups
options['protectclock']="true" # option : systemd protect clock
options['protectproc']="invisible" # option : systemd protect proc
options['procsubset']="pid" # option : systemd protect kernel modules
options['privatetmp']="true" # option : systemd private tmp
options['memorydenywriteexecute']="true" # option : systemd deny write execute
options['nownewprivileges']="true" # option : systemd no new privileges
options['lockpersonality']="true" # option : systemd lock personality
options['restrictrealtime']="true" # option : systemd restrict realtime
options['systemcallarchitectures']="native" # option : systemd system call architectures
options['ipaddressdeny']="any" # option : systemd IP address deny
options['usepreservediso']="false" # option : Use preserved ISO
options['logrotate']="true" # option : Log rotate
options['unstable']="false" # option : Enable unstable features/packages
options['interactive']="false" # option : Interactive mode
options['interactiveinstall']="false" # option : Interactive install mode
options['allowbroken']="false" # option : Allow broken packages
options['usbstick']="" # option : USB stick to write ISO to
options['cdrom']="" # option : ISO to use (e.g. for writing to usb stick)
options['format']="install-iso" # option : Output format
options['imageformat']="iso" # option : Image format
options['imagesuffix']="iso" # option : Image suffix
options['compressimage']="true" # option : Compress image
options['gc']="true" # option : Garbage collection
# Process GRUB options - Set this to false if you have changed parameters above
options['processgrub']="true" # option : Process grub command line
# VM defaults
vm['name']="${script['name']}" # vm : VM name
vm['vcpus']="2" # vm : VM vCPUs
vm['cpu']="host-passthrough" # vm : VM CPU
vm['os-variant']="nixos-unknown" # vm : VM OS variant
vm['host-device']="" # vm : VM Host-device for pass-through
vm['graphics']="none" # vm : VM Graphics
vm['virt-type']="kvm" # vm : VM Virtualisation type
vm['network']="bridge=br0,model=virtio" # vm : VM NIC
vm['memory']="4G" # vm : VM RAM
vm['cdrom']="" # vm : VM ISO
vm['boot']="uefi" # vm : VM Boot type
vm['disk']="" # vm : VM Disk
vm['features']="kvm_hidden=on" # vm : VM Features
vm['size']="20G" # vm : VM Disk size
vm['dir']="${options['workdir']}/vms" # vm : VM Directory
vm['noautoconsole']="true" # vm : VM Autoconsole
vm['noreboot']="true" # vm : VM Reboot
vm['wait']="" # vm : VM Wait before starting
vm['machine']="q35" # vm : VM Machine
os['name']=$( uname -s )
if [ "${os['name']}" = "Linux" ]; then
lsb_check=$( command -v lsb_release )
if [ -n "${lsb_check}" ]; then
os['distro']=$( lsb_release -i -s 2 | sed 's/"//g' 2>&1 /dev/null )
else
os['distro']=$( hostnamectl | grep "Operating System" | awk '{print $3}' )
fi
fi
if [ "${os['name']}" = "Darwin" ]; then
vm['cpu']="cortex-a57"
vm['network']="default,model=virtio"
fi
}
set_defaults
# Function: print_message
#
# Print message
print_message () {
message="$1"
format="$2"
if [ "${format}" = "verbose" ]; then
echo "${message}"
else
if [[ "${format}" =~ warn ]]; then
echo -e "Warning:\t${message}"
else
if [ "${options['verbose']}" = "true" ]; then
if [[ "${format}" =~ ing$ ]]; then
format="${format^}"
else
if [[ "${format}" =~ t$ ]]; then
if [ "${format}" = "test" ]; then
format="${format}ing"
else
format="${format^}ting"
fi
else
if [[ "${format}" =~ e$ ]]; then
if ! [[ "${format}" =~ otice ]]; then
format="${format::-1}"
format="${format^}ing"
fi
fi
fi
fi
format="${format^}"
length="${#format}"
if [ "${length}" -lt 7 ]; then
tabs="\t\t"
else
tabs="\t"
fi
echo -e "${format}:${tabs}${message}"
fi
fi
fi
}
# Function: verbose_message
#
# Verbose message
verbose_message () {
message="$1"
print_message "${message}" "verbose"
}
# Function: warning_message
#
# Warning message
warning_message () {
message="$1"
print_message "${message}" "warn"
}
# Function: execute_message
#
# Print command
execute_message () {
message="$1"
print_message "${message}" "execute"
}
# Function: notice_message
#
# Notice message
notice_message () {
message="$1"
verbose_message "${message}" "notice"
}
# Function: notice_message
#
# Information Message
information_message () {
message="$1"
verbose_message "${message}" "info"
}
# Load modules
if [ -d "${script['modulepath']}" ]; then
modules=$( find "${script['modulepath']}" -name "*.sh" )
for module in ${modules}; do
if [[ "${script['args']}" =~ "verbose" ]]; then
print_message "Module ${module}" "load"
fi
. "${module}"
done
fi
# Function: reset_defaults
#
# Reset defaults based on command line options
reset_defaults () {
# Imports
if [ "${options['format']}" = "sd" ]; then
options['isoimports']="" # option : ISO imports
isoimports=(
"<nixpkgs/nixos/modules/installer/sd-card/sd-image-aarch64-installer.nix>"
"<nixpkgs/nixos/modules/installer/cd-dvd/channel.nix>"
"<nixpkgs/nixos/modules/system/boot/loader/grub/grub.nix>"
"<nixpkgs/nixos/modules/system/boot/kernel.nix>"
)
options['isoimports']="${isoimports[*]}"
options['isoimports']="${options['isoimports']} <nixpkgs/nixos/modules/installer/sd-card/sd-image-aarch64-installer.nix>"
options['imageformat']="img"
if [ "${options['compressimage']}" = "true" ]; then
options['imagesuffix']="img.dst"
else
options['imagesuffix']="img"
fi
fi
# Process compession options
if [ "${options['compression']}" = "" ]; then
if [ "${options['rootfs']}" = "btrfs" ]; then
options['compression']="zstd:3"
else
options['compression']="lz4"
fi
fi
zfsoptions=(
"-O mountpoint=none"
"-O atime=off"
"-O compression=${options['compression']}"
"-O xattr=sa"
"-O acltype=posixacl"
"-o ashift=12"
)
options['zfsoptions']="${zfsoptions[*]}"
btrfsoptions=(
"rw"
"relatime"
"compress=${options['compression']}"
"discard=async"
"space_cache=v2"
)
options['btrfsoptions']="${btrfsoptions[*]}"
# Process hostname and domainname
if [[ "${options['hostname']}" =~ . ]]; then
if [ "${options['domainname']}" = "" ]; then
options['domainname']=$( echo "${options['hostname']}" | cut -f2- -d. )
fi
options['hostname']=$( echo "${options['hostname']}" | cut -f1 -d. )
fi
if [ "${options['unstable']}" = "true" ]; then
options['isostorepackages']="${options['isostorepackages']} zfs_unstable"
options['isossystempackages']="${options['isosystempackages']} zfs_unstable"
options['systempackages']="${options['systempackages']} zfs_unstable"
fi
# Process serial options
serial_console="console=${options['serialtty']},${options['serialspeed']}${options['serialparity']}${options['serialword']}"
serialkernelparams=(
"console=tty1"
"${serial_console}"
)
for item in "${serialkernelparams[@]}"; do
options['isoserialkernelparams']+=" \"${item}\" "
done
for item in "${serialkernelparams[@]}"; do
options['serialkernelparams']+=" ${item} "
done
serial_params="serial"
for param in speed unit word parity stop port; do
item="serial${param}"
value="${options[${item}]}"
if ! [ "${value}" = "" ]; then
serial_params="${serial_params} --${param}=${value}"
fi
done
serialextraconfig=(
"${serial_params}"
"terminal_input serial"
"terminal_output serial"
)
for item in "${serialextraconfig[@]}"; do
if [ "${options['serialextraconfig']}" = "" ]; then
options['serialextraconfig']=" ${item} "
else
options['serialextraconfig']+=" ${item} "
fi
done
# Process install options
if [ "${options['attended']}" = "true" ]; then
options['unattended']="false"
fi
if [ "${options['unattended']}" = "true" ]; then
options['attended']="false"
fi
if [ "${options['reboot']}" = "true" ]; then
options['poweroff']="false"
fi
if [ "${options['poweroff']}" = "true" ]; then
options['reboot']="false"
fi
if ! [ "${options['allowusers']}" = "${options['username']}" ]; then
options['allowusers']="${options['username']}"
fi
# System sysctl parameters - Security related
if [ "${options['audit']}" = "true" ]; then
IFS='' read -r -d '' options['auditrules'] << SYSCTL
"-a exit,always -F arch=b64 -S execve"
"-a always,exit -F arch=b32 -S adjtimex,settimeofday,clock_settime,stime -k time-change"
"-a always,exit -F arch=b64 -S adjtimex,settimeofday,clock_settime -k time-change"
"-w /etc/localtime -p wa -k time-change"
"-w /etc/group -p wa -k identity"
"-w /etc/passwd -p wa -k identity"
"-w /etc/gshadow -p wa -k identity"
"-w /etc/shadow -p wa -k identity"
"-a exit,always -F arch=b32 -S sethostname,setdomainname -k system-locale"
"-a exit,always -F arch=b64 -S sethostname,setdomainname -k system-locale"
"-w /etc/issue -p wa -k system-locale"
"-w /etc/issue.net -p wa -k system-locale"
"-w /etc/hosts -p wa -k system-locale"
"-w /etc/apparmor/ -p wa -k MAC-policy"
"-w /etc/apparmor.d/ -p wa -k MAC-policy"
"-w /var/log/faillog -p wa -k logins"
"-w /var/log/lastlog -p wa -k logins"
"-w /var/run/faillock -p wa -k logins"
"-w /var/run/utmp -p wa -k session"
"-w /var/log/btmp -p wa -k session"
"-w /var/log/wtmp -p wa -k session"
"-a always,exit -F path=/usr/bin/setfacl -F perm=x -F auid>=1000 -F auid!=unset -k perm_chng"
"-a always,exit -F arch=b32 -C euid!=uid -F auid!=unset -S execve -k user_emulation"
"-a always,exit -F arch=b64 -C euid!=uid -F auid!=unset -S execve -k user_emulation"
"-a always,exit -F arch=b32 -S chmod -S fchmod -S fchmodat -F auid>=500 -F auid!=4294967295 -k perm_mod"
"-a always,exit -F arch=b64 -S chmod -S fchmod -S fchmodat -F auid>=500 -F auid!=4294967295 -k perm_mod"
"-a always,exit -F arch=b32 -S setxattr -S lsetxattr -S fsetxattr -S removexattr -S lremovexattr -S fremovexattr -F auid>=500 -F auid!=4294967295 -k perm_mod"
"-a always,exit -F arch=b64 -S setxattr -S lsetxattr -S fsetxattr -S removexattr -S lremovexattr -S fremovexattr -F auid>=500 -F auid!=4294967295 -k perm_mod"
"-a always,exit -F arch=b32 -S creat -S open -S openat -S truncate -S ftruncate -F exit=-EACCES -F auid>=500 -F auid!=4294967295 -k access"
"-a always,exit -F arch=b32 -S creat -S open -S openat -S truncate -S ftruncate -F exit=-EPERM -F auid>=500 -F auid!=4294967295 -k access"
"-a always,exit -F arch=b64 -S creat -S open -S openat -S truncate -S ftruncate -F exit=-EACCES -F auid>=500 -F auid!=4294967295 -k access"
"-a always,exit -F arch=b64 -S creat -S open -S openat -S truncate -S ftruncate -F exit=-EPERM -F auid>=500 -F auid!=4294967295 -k access"
"-a always,exit -F path=/usr/bin/chacl -F perm=x -F auid>=1000 -F auid!=unset -k priv_cmd"
"-a always,exit -F arch=b32 -S mount -F auid>=500 -F auid!=4294967295 -k export"
"-a always,exit -F arch=b64 -S mount -F auid>=500 -F auid!=4294967295 -k export"
"-a always,exit -F arch=b32 -S unlink -S unlinkat -S rename -S renameat -F auid>=500 -F auid!=4294967295 -k delete"
"-a always,exit -F arch=b64 -S unlink -S unlinkat -S rename -S renameat -F auid>=500 -F auid!=4294967295 -k delete"
"-w /etc/sudoers -p wa -k scope"
"-w /etc/sudoers.d -p wa -k scope"
"-w /etc/sudoers -p wa -k actions"
"-w /var/log/sudo.log -p wa -k sudo_log_file"
"-w /run/current-system/sw/bin/insmod -p x -k modules"
"-w /run/current-system/sw/bin/rmmod -p x -k modules"
"-w /run/current-system/sw/bin/modprobe -p x -k modules"
"-a always,exit -F arch=b64 -S init_module,finit_module,delete_module,create_module,query_module -F auid>=1000 -F auid!=unset -k kernel_modules"
"-a always,exit -S init_module -S delete_module -k modules"
"-a always,exit -F arch=b64 -S mount -F auid>=500 -F auid!=4294967295 -k mounts"
"-a always,exit -F arch=b32 -S mount -F auid>=500 -F auid!=4294967295 -k mounts"
SYSCTL
options['auditrules']="${options['auditrules']//\"/\\\"}"
fi
if [ "${options['secure']}" = "true" ]; then
kernelparams=(
"audit=1"
"slab_nomerge"
"init_on_alloc=1"
"init_on_free=1"
"page_alloc.shuffel=1"
"pti=on"
"randomize_kstack_offset=on"
"vsyscall=none"
"debugfs=off"
"oops=panic"
"module.sig_enforce=1"
"lockdown=confidentiality"
"rd.udev.log_level=3"
"udev.log_priority=3"
)
options['kernelparams']="${kernelparams[*]}"
IFS='' read -r -d '' options['sysctl'] << SYSCTL
"kernel.exec-shield" = 1;
"net.ipv4.tcp_rfc1337" = 1;
"net.ipv6.conf.all.forwarding" = 0;
"net.ipv4.conf.all.accept_redirects" = 0;
"net.ipv4.conf.all.secure_redirects" = 0;
"kernel.dmesg_restrict" = 1;
"kernel.randomize_va_space" = 2;
"net.ipv4.conf.default.secure_redirects" = 0;
"net.ipv4.conf.all.rp_filter" = 1;
"net.ipv6.conf.default.accept_ra" = 0;
"net.ipv4.conf.default.accept_source_route" = 0;
"net.ipv4.icmp_ignore_bogus_error_responses" = 1;
"fs.protected_hardlinks" = 1;
"kernel.yama.ptrace_scope" = 2;
"dev.tty.ldisk_autoload" = 0;
"kernel.unprivileged_bpf_disabled" = 1;
"net.ipv4.conf.all.forwarding" = 0;
"fs.suid_dumpable" = 0;
"vm.mmap_rnd_compat_bits" = 16;
"net.ipv6.conf.all.accept_ra" = 0;
"net.ipv4.conf.default.rp_filter" = 1;
"fs.protected_regular" = 2;
"net.ipv4.conf.all.accept_source_route" = 0;
"net.ipv4.tcp_dsack" = 0;
"vm.unprivileged_userfaultfd" = 0;
"net.ipv4.conf.all.send_redirects" = 0;
"fs.protected_fifos" = 2;
"net.ipv4.tcp_fack" = 0;
"net.ipv4.tcp_syncookies" = 1;
"net.ipv4.icmp_echo_ignore_all" = 1;
"kernel.perf_event_paranoid" = 3;
"net.core.default_qdisc" = "cake";
"net.ipv4.tcp_sack" = 0;
"net.ipv4.conf.default.send_redirects" = 0;
"net.ipv4.conf.default.accept_redirects" = 0;
"net.ipv4.tcp_congestion_control" = "bbr";
"net.core.bpf_jit_harden" = 2;
"net.ipv6.conf.all.accept_source_route" = 0;
"kernel.kptr_restrict" = 2;
"fs.protected_symlinks" = 1;
"net.ipv6.conf.default.accept_source_route" = 0;
"kernel.sysrq" = 4;
"kernel.kexec_load_disabled" = 1;
"net.ipv6.conf.default.accept_redirects" = 0;
"vm.mmap_rnd_bits" = 32;
"net.ipv4.tcp_fastopen" = 3;
"net.ipv6.conf.all.accept_redirects" = 0;
SYSCTL
options['sysctl']="${options['sysctl']//\"/\\\"}"
fi
if [ "${options['debug']}" = "true" ]; then
print_message "Enabling debug mode" "notice"
set -x
fi
if [ "${options['strict']}" = "true" ]; then
print_message "Enabling strict mode" "notice"
set -u
fi
if [ "${options['dryrun']}" = "true" ]; then
print_message "Enabling dryrun mode" "notice"
fi
if [ "${options['zsh']}" = "true" ]; then
options['usershell']="zsh"
fi
if ! [ "${options['usershell']}" = "zsh" ]; then
options['zsh']="false"
fi
if ! [ "${options['rootfs']}" = "zfs" ]; then
options['zfs']="false"
if [ "${options['rootfs']}" = "btrfs" ]; then
options['lvm']="false"
else
options['lvm']="true"
fi
fi
if [ "${options['lvm']}" = "true" ]; then
options['zfs']="false"
if [ "${options["rootfs"]}" = "zfs" ]; then
warning_message "LVM selected, root filestem cannot be ZFS, setting to EXT4"
options['rootfs']="ext4"
fi
fi
if [ "${options['zfs']}" = "true" ] || [ "${options['rootfs']}" = "zfs" ]; then
options['zfs']='true'
options['rootfs']='zfs'
fi
if [ "${options['ext4']}" = "true" ] || [ "${options['rootfs']}" = "ext4" ]; then
options['ext4']='true'
options['rootfs']='ext4'
fi
if [ "${options['bios']}" = "true" ] || [ "${options['firmware']}" = "bios" ]; then
options['bios']='true'
options['firmware']='bios'
fi
if [ "${options['uefi']}" = "true" ] || [ "${options['firmware']}" = "uefi" ]; then
options['uefi']='true'
options['firmware']='uefi'
fi
if [ "${options['serial']}" = "true" ]; then
if [ "${options['isokernelparams']}" = "" ]; then
options['isokernelparams']="${options['isoserialkernelparams']}"
else
options['isokernelparams']="${options['isokernelparams']} ${options['isoserialkernelparams']}"
fi
if [ "${options['kernelparams']}" = "" ]; then
options['kernelparams']="${options['serialkernelparams']}"
else
options['kernelparams']="${options['kernelparams']} ${options['serialkernelparams']}"
fi
if [ "${options['isogrubextraconfig']}" = "" ]; then
options['isogrubextraconfig']="${options['serialextraconfig']}"
else
options['isogrubextraconfig']="${options['isogrubextraconfig']} ${options['serialextraconfig']}"
fi
if [ "${options['grubextraconfig']}" = "" ]; then
options['grubextraconfig']="${options['serialextraconfig']}"
else
options['grubextraconfig']="${options['grubextraconfig']} ${options['serialextraconfig']}"
fi
fi
if [[ ${options['kernel']} =~ latest ]] && [[ ${options['kernel']} =~ hardened ]]; then
options['kernel']="_latest_hardened"
else
if [[ ${options['kernel']} =~ latest ]]; then
options['kernel']="_latest"
else
if [[ ${options['kernel']} =~ hardened ]]; then
options['kernel']="_hardened"
fi
fi
fi
if ! [ "${options['kernel']}" = "" ]; then
if ! [[ "${options['kernel']}" =~ ^_ ]]; then
options['kernel']="_${options['kernel']}"
fi
options['kernel']="${options['kernel']//\./_}"
fi
if ! [ "${options['import']}" = "" ]; then
if ! [ -f "${options['import']}" ]; then
warning_message "Nix configuration file ${options['import']} does not exist"
do_exit
else
import=$( basename "${options['import']}" )
execute_command "cp ${options['import']} ${options['source']}"
fi
options['imports']="${options['import']} ./${import}"
fi
if ! [ "${options['isoimport']}" = "" ]; then
if ! [ -f "${options['isoimport']}" ]; then
warning_message "Nix configuration file ${options['isoimport']} does not exist"
do_exit
else
options['isoimports']="${options['isoimport']} ${import}"
fi
fi
if [ "${options['latest']}" = "true" ]; then
options['stateversion']="${options['latestversion']}"
fi
}
# Function: do_exit
#
# Selective exit (don't exit when we're running in dryrun mode)
do_exit () {
if [ "${options['dryrun']}" = "false" ]; then
exit
fi
}
# Function: check_value
#
# check value (make sure that command line arguments that take values have values)
check_value () {
param="$1"
value="$2"
if [[ ${value} =~ ^-- ]]; then
print_message "Value '$value' for parameter '$param' looks like a parameter" "verbose"
echo ""
if [ "${options['force']}" = "false" ]; then
do_exit
fi
else
if [ "${value}" = "" ]; then
print_message "No value given for parameter $param" "verbose"
echo ""
if [[ "${param}" =~ "option" ]]; then
print_options
else