-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathnvidiainstall.sh
More file actions
executable file
·1002 lines (846 loc) · 32.4 KB
/
Copy pathnvidiainstall.sh
File metadata and controls
executable file
·1002 lines (846 loc) · 32.4 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
#!/usr/bin/env bash
#
# Automated NVIDIA Driver Installer for Arch Linux
#
# Author: Justus0405
# Date: 12.10.2024
# License: MIT
export scriptVersion="2.5"
export legacyMode="false"
### COLOR CODES ###
export black="\e[1;30m"
export red="\e[1;31m"
export green="\e[1;32m"
export yellow="\e[1;33m"
export blue="\e[1;34m"
export purple="\e[1;35m"
export cyan="\e[1;36m"
export lightGray="\e[1;37m"
export gray="\e[1;90m"
export lightRed="\e[1;91m"
export lightGreen="\e[1;92m"
export lightYellow="\e[1;93m"
export lightBlue="\e[1;94m"
export lightPurple="\e[1;95m"
export lightCyan="\e[1;96m"
export white="\e[1;97m"
export bold="\e[1m"
export faint="\e[2m"
export italic="\e[3m"
export underlined="\e[4m"
export blinking="\e[5m"
export reset="\e[0m"
### FUNCTIONS ###
logMessage() {
local type="$1"
local message="$2"
case "${type}" in
"info" | "INFO")
echo -e "${gray}[${cyan}i${gray}]${reset} ${message}"
;;
"done" | "DONE")
echo -e "${gray}[${green}✓${gray}]${reset} ${message}"
exit 0
;;
"warning" | "WARNING")
echo -e "${gray}[${red}!${gray}]${reset} ${message}"
;;
"error" | "ERROR")
echo -e "${red}ERROR${reset}: ${message}"
exit 1
;;
*)
echo -e "[UNDEFINED] ${message}"
;;
esac
}
getArguments() {
case "$1" in
"-l" | "--legacy")
export legacyMode="true"
;;
"-h" | "--help")
printHelp
;;
"-v" | "--version")
printVersion
;;
"")
return
;;
*)
return
;;
esac
}
checkSudo() {
# Checks EUID to see if the script is running as sudo.
if [[ "$EUID" != 0 ]]; then
logMessage "error" "This script must be run as root. Use sudo."
fi
# Looks if the root user is permitted to execute ommands as sudo,
# this is needed because executing commands with privilges in a bash script is a bit weird.
# Or it may be just a skill issue. ¯\_(ツ)_/¯
usermod -aG wheel root || logMessage "error" "Failed to add root to the wheel group."
}
checkAurHelper() {
# WARNING! DEPRICATED SINCE v2.4
# Checking if yay is installed.
if command -v yay >/dev/null 2>&1; then
logMessage "info" "Yay is installed."
else
logMessage "info" "Yay is not installed."
installAurHelper
fi
}
installAurHelper() {
# WARNING! DEPRICATED SINCE v2.4
# Installing yay as aur helper for the executing user.
# Makepkg crashes if its not running as a non-root user
targetUser="${SUDO_USER:-$(whoami)}"
logMessage "info" "Installing yay..."
sudo -u "${targetUser}" bash <<'EOF'
cd /tmp
git clone https://aur.archlinux.org/yay.git
cd yay
makepkg -si --noconfirm
EOF
logMessage "info" "Sucessfully installed yay."
}
aurHelperInstall() {
# WARNING! DEPRICATED SINCE v2.4
# Install with yay using a non-root user.
# This is because yay complains when running as root.
local packages="$1"
local targetUser="${SUDO_USER:-$(whoami)}"
# shellcheck disable=SC2086
sudo -u "${targetUser}" yay -S --needed --noconfirm ${packages}
}
aurHelperUninstall() {
# WARNING! DEPRICATED SINCE v2.4
# Uninstall with yay using a non-root user.
# This is because yay complains when running as root.
local packages="$1"
local targetUser="${SUDO_USER:-$(whoami)}"
# shellcheck disable=SC2086
sudo -u "${targetUser}" yay -R --noconfirm ${packages}
}
checkChaoticAur() {
# Checking if the chaotic aur is installed and configured
if [[ -f "/etc/pacman.d/chaotic-mirrorlist" ]]; then
logMessage "info" "Chaotic AUR is installed."
else
logMessage "warning" "Chaotic AUR is not installed."
installChaoticAUR
fi
}
installChaoticAUR() {
# Install the chaotic aur mirrorlist
logMessage "info" "Installing the Chaotic AUR..."
sudo pacman-key --recv-key 3056513887B78AEB --keyserver keyserver.ubuntu.com
sudo pacman-key --lsign-key 3056513887B78AEB
sudo pacman -U --noconfirm 'https://cdn-mirror.chaotic.cx/chaotic-aur/chaotic-keyring.pkg.tar.zst'
sudo pacman -U --noconfirm 'https://cdn-mirror.chaotic.cx/chaotic-aur/chaotic-mirrorlist.pkg.tar.zst'
sudo tee -a "/etc/pacman.conf" >/dev/null <<EOF
[chaotic-aur]
Include = /etc/pacman.d/chaotic-mirrorlist
EOF
sudo pacman -Syy
logMessage "info" "Successfully installed the Chaotic AUR."
}
checkNvidia() {
# Detect NVIDIA GPU and decide driver package.
# Index: https://www.techpowerup.com/gpu-specs/
# Default values.
gpuGen="Unknown"
gpuDriver="Unknown"
gpuInfo=$(lspci -nn | grep -i 'VGA.*NVIDIA')
gpuName=$(echo "${gpuInfo}" | sed -E 's/.*NVIDIA Corporation //; s/ \[.*//')
case "${gpuName}" in
*"GB10"* | *"GB20"*)
gpuGen="Blackwell"
gpuDriver="nvidia-open-dkms"
;;
*"GH10"*)
gpuGen="Hopper"
gpuDriver="nvidia-open-dkms"
;;
*"AD10"*)
gpuGen="Ada Lovelace"
gpuDriver="nvidia-open-dkms"
;;
*"GA10"*)
gpuGen="Ampere"
gpuDriver="nvidia-open-dkms"
;;
*"TU10"* | *"TU11"*)
gpuGen="Turing"
gpuDriver="nvidia-open-dkms"
;;
*"GV10"*)
gpuGen="Volta"
gpuDriver="nvidia-580xx-dkms"
;;
*"GP10"*)
gpuGen="Pascal"
gpuDriver="nvidia-580xx-dkms"
;;
*"GM10"* | *"GM20"*)
gpuGen="Maxwell"
gpuDriver="nvidia-580xx-dkms"
;;
*"EXK107"* | *"GK10"* | *"GK11"* | *"GK18"* | *"GK20"* | *"GK21"*)
gpuGen="Kepler"
gpuDriver="nvidia-470xx-dkms"
;;
*"EXMF1"* | *"GF10"* | *"GF11"*)
gpuGen="Fermi"
gpuDriver="nvidia-390xx-dkms"
;;
*"Kal-El"* | *"Tegra 2"* | *"Wayne"*)
gpuGen="VLIW Vec4"
gpuDriver="nvidia-390xx-dkms"
;;
*"C77"* | *"C78"* | *"C79"* | *"C7A"* | *"G80"* | *"G84"* | *"G86"* | *"G92"* | *"G94"* | *"G96"* | *"G98"* | *"ION"* | *"C87"* | *"C89"* | *"GT20"* | *"GT21"*)
# C7A-ION, NVIDIA ION
gpuGen="Tesla"
gpuDriver="nvidia-340xx-dkms"
;;
*"C51"* | *"C61"* | *"C67"* | *"C68"* | *"C73"* | *"G70"* | *"G71"* | *"G72"* | *"G73"* | *"NV40"* | *"NV41"* | *"NV42"* | *"NV43"* | *"NV44"* | *"NV45"* | *"NV48"* | *"RSX"*)
gpuGen="Curie"
gpuDriver="unsupported"
;;
*"NV30"* | *"NV31"* | *"NV34"* | *"NV35"* | *"NV36"* | *"NV37"* | *"NV38"* | *"NV39"*)
gpuGen="Rankine"
gpuDriver="unsupported"
;;
*"NV20"* | *"NV25"* | *"NV28"* | *"NV2A"*)
gpuGen="Kelvin"
gpuDriver="unsupported"
;;
*"Crush1"* | *"NV10"* | *"NV11"* | *"NV15"* | *"NV17"* | *"NV18"*)
gpuGen="Celsius"
gpuDriver="unsupported"
;;
*"NV4"* | *"NV5"*)
gpuGen="Fahrenheit"
gpuDriver="unsupported"
;;
*)
gpuGen="Unknown"
gpuDriver="unidentified"
;;
esac
if [[ ${gpuDriver} == "unsupported" ]]; then
logMessage "error" "${gpuGen} is not supported anymore."
fi
if [[ ${gpuDriver} == "unidentified" ]]; then
chooseGpuDriver
fi
if [[ -z ${gpuName} ]]; then
gpuName="Unkown"
fi
}
checkInstalledDriver() {
# This function checks if any nvidia drivers are installed and serves them
# inside a variable in order for the uninstallation step to know which packages to remove.
# Is also used in showDeviceInformation().
legacyDriver=$(pacman -Qq | grep -E '^nvidia$')
installedDriver=$(pacman -Qq | grep -E 'nvidia-(dkms|open-dkms|470xx-dkms|390xx-dkms|340xx-dkms)')
if [[ -n ${legacyDriver} ]]; then
installedDriver="nvidia"
fi
if [[ -z ${installedDriver} ]]; then
installedDriver="none"
fi
}
chooseGpuDriver() {
# In case the script couldnt identify the needed driver,
# ask the user which one they want to install.
clear
echo -e "\t┌──────────────────────────────────────────────────┐"
echo -e "\t│ / \ │"
echo -e "\t│ / | \ We could not identify your GPU. │"
echo -e "\t│ / # \ Please select which driver you │"
echo -e "\t│ /_______\ want to manage. │"
echo -e "\t│ │"
echo -e "\t│ [!] Curie and older are not supported anymore! │"
echo -e "\t├──────────────────────────────────────────────────┤"
echo -e "\t│ │"
echo -e "\t│ [1] nvidia-open-dkms [Turing and newer] │"
echo -e "\t│ [2] nvidia-580xx-dkms [Maxwell, Pascal, Volta] │"
echo -e "\t│ [3] nvidia-470xx-dkms [Kepler] │"
echo -e "\t│ [4] nvidia-390xx-dkms [Fermi] │"
echo -e "\t│ [5] nvidia-340xx-dkms [Tesla] │"
echo -e "\t│ │"
echo -e "\t├──────────────────────────────────────────────────┤"
echo -e "\t│ [0] Quit │"
echo -e "\t└──────────────────────────────────────────────────┘"
echo -e ""
echo -e "\t${green}Choose a menu option using your keyboard [1,2,...,0]${reset}"
read -rsn1 option
case "${option}" in
"1")
gpuDriver="nvidia-open-dkms"
;;
"2")
gpuDriver="nvidia-580xx-dkms"
;;
"3")
gpuDriver="nvidia-470xx-dkms"
;;
"4")
gpuDriver="nvidia-390xx-dkms"
;;
"5")
gpuDriver="nvidia-340xx-dkms"
;;
"0")
exitScript "Quit."
;;
*)
chooseGpuDriver
;;
esac
}
backupConfig() {
# Create a copy of the given file with the .bak extention.
local config="$1"
logMessage "info" "Creating backup of ${config}"
sudo cp "${config}" "${config}.bak"
logMessage "info" "Backup of ${config} created."
}
### TERMINAL INTERFACE ###
showMenu() {
# This is the main function which renders the selection menu.
# Waiting for the input of the user for running further functions.
# The function runs itself at the end ensuring coming back to it
# when the selected option finished running.
clear
echo -e "\t┌──────────────────────────────────────────────────┐"
echo -e "\t│ │"
echo -e "\t│ Choose option: │"
echo -e "\t│ │"
echo -e "\t│ [1] Install │"
echo -e "\t│ [2] Uninstall │"
echo -e "\t│ [3] Device Information │"
echo -e "\t│ [4] About Nvidiainstall │"
echo -e "\t│ │"
echo -e "\t├──────────────────────────────────────────────────┤"
echo -e "\t│ [0] Quit │"
echo -e "\t└──────────────────────────────────────────────────┘"
echo -e ""
echo -e "\t${green}Choose a menu option using your keyboard [1,2,...,0]${reset}"
read -rsn1 option
case "${option}" in
"1")
if [[ ${installedDriver} == "none" ]]; then
confirmInstallation
else
showDriverInstalled
fi
;;
"2")
if [[ ${installedDriver} == "none" ]]; then
showNoDriverInstalled
else
confirmUninstallation
fi
;;
"3")
showDeviceInformation
;;
"4")
showAbout
;;
"0")
exitScript "Quit."
;;
*)
showMenu
;;
esac
# Loop back to menu after an option is handled.
showMenu
}
showDeviceInformation() {
# Show information about gpu name, generation and recommended driver.
clear
echo -e ""
echo -e "\tDevice Information:"
echo -e ""
echo -e "\tDetected GPU: ${gpuName}"
echo -e "\tGeneration: ${gpuGen}"
echo -e "\tInstalled Driver: ${installedDriver}"
echo -e ""
echo -e "\tSelected Driver: ${gpuDriver}"
echo -e "\tLegacy Mode: ${legacyMode}"
echo -e ""
echo -e "\t${green}Press any button to return${reset}"
read -rsn1 option
case "${option}" in
*) ;;
esac
}
showAbout() {
# Just a bit of info.
# Also fetches the list of contributers regarding this project and displays them in a list.
githubResponse=$(curl -s "https://api.github.com/repos/Justus0405/Nvidiainstall/contributors")
clear
echo -e ""
echo -e "\tAbout Nvidiainstall:"
echo -e ""
echo -e "\tVersion: ${scriptVersion}"
echo -e "\tAuthor : Justus0405"
echo -e "\tSource : https://github.com/Justus0405/Nvidiainstall"
echo -e "\tLicense: MIT"
echo -e "\tContributors:"
echo "${githubResponse}" | grep '"login":' | awk -F '"' '{print $4}' | while read -r contributors; do
echo -e "\t\t\e[0;35m${contributors}\e[m"
done
echo -e ""
echo -e "\t${green}Press any button to return${reset}"
read -rsn1 option
case "${option}" in
*) ;;
esac
}
showDriverInstalled() {
# Screen for when the user wants to install nvidia drivers but others are found.
clear
echo -e "\t┌──────────────────────────────────────────────────┐"
echo -e "\t│ / \ │"
echo -e "\t│ / | \ You already have other NVIDIA dkms │"
echo -e "\t│ / # \ packages Installed! │"
echo -e "\t│ /_______\ │"
echo -e "\t└──────────────────────────────────────────────────┘"
echo -e ""
echo -e "\tInstalled Package: ${installedDriver}"
echo -e ""
echo -e "\t${green}Press any button to return${reset}"
read -rsn1 option
case "${option}" in
*) ;;
esac
}
showNoDriverInstalled() {
# Screen for when the user wants to uninstall nvidia drivers but none could be found.
clear
echo -e "\t┌──────────────────────────────────────────────────┐"
echo -e "\t│ / \ │"
echo -e "\t│ / | \ We could not find any installed │"
echo -e "\t│ / # \ NVIDIA dkms packages! │"
echo -e "\t│ /_______\ │"
echo -e "\t└──────────────────────────────────────────────────┘"
echo -e ""
echo -e "\t${green}Press any button to return${reset}"
read -rsn1 option
case "${option}" in
*) ;;
esac
}
### INSTALLATION STEPS ###
confirmInstallation() {
# Ask the user for consent :3
clear
echo -e "\t┌──────────────────────────────────────────────────┐"
echo -e "\t│ / \ │"
echo -e "\t│ / | \ This script will install NVIDIA │"
echo -e "\t│ / # \ drivers and modify system │"
echo -e "\t│ /_______\ configurations. │"
echo -e "\t│ │"
echo -e "\t│ [!] Proceed with caution! │"
echo -e "\t└──────────────────────────────────────────────────┘"
echo -e ""
read -rp "Do you want to install ${gpuDriver}? (y/N): " confirm
case "${confirm}" in
[yY][eE][sS] | [yY])
echo -e "${green}Installing ${gpuDriver}...${reset}"
installationSteps
;;
*)
exitScript "Installation cancelled."
;;
esac
}
installationSteps() {
# Just a simple function handling each steps because
# handling it everywere else looked ugly.
# Step 1
updateSystem
# Step 2
checkKernelHeaders
# Step 3
installNvidiaPackages
# Step 4
configureMkinitcpio
# Step 5
configureModprobe
# Step 6
configureGrubDefault
# Step 7
regenerateInitramfs
# Step 8
updateGrubConfig
# Step 9
confirmReboot
}
updateSystem() {
# Updating system because why not?
logMessage "info" "Updating System..."
sudo pacman -Syyu --noconfirm
logMessage "info" "Updated System."
}
checkKernelHeaders() {
# Check the installed kernel and installs the associated headers.
# this is needed for the kernel to load the nvidia modules.
logMessage "info" "Installing Kernel Modules..."
kernel=$(uname -r)
if [[ "${kernel}" == *"zen"* ]]; then
# Zen
logMessage "info" "Detected Kernel: linux-zen"
sudo pacman -S --needed --noconfirm linux-zen-headers
elif [[ "${kernel}" == *"lts"* ]]; then
# LTS
logMessage "info" "Detected Kernel: linux-lts"
sudo pacman -S --needed --noconfirm linux-lts-headers
elif [[ "$kernel" == *"hardened"* ]]; then
# "HARDENED" ~Debitor
logMessage "info" "Detected Kernel: linux-hardened"
sudo pacman -S --needed --noconfirm linux-hardened-headers
else
# Regular Linux Kernel
logMessage "info" "Detected Kernel: linux"
sudo pacman -S --needed --noconfirm linux-headers
fi
logMessage "info" "Installed Kernel Modules."
}
installNvidiaPackages() {
# Install the nvidia drivers and needed dependencies, if not present.
logMessage "info" "Identified Generation: ${gpuGen}"
logMessage "info" "Installing ${gpuDriver} and dependencies..."
case "${gpuDriver}" in
"nvidia-open-dkms")
sudo pacman -S --needed --noconfirm nvidia-open-dkms nvidia-utils opencl-nvidia nvidia-settings libglvnd lib32-nvidia-utils lib32-opencl-nvidia egl-wayland
;;
"nvidia-580xx-dkms")
if [[ "${legacyMode}" == "true" ]]; then
checkAurHelper
aurHelperInstall "nvidia-580xx-dkms nvidia-580xx-utils opencl-nvidia-580xx nvidia-580xx-settings libglvnd lib32-nvidia-580xx-utils lib32-opencl-nvidia-580xx egl-wayland"
else
checkChaoticAur
sudo pacman -S --needed --noconfirm nvidia-580xx-dkms nvidia-580xx-utils opencl-nvidia-580xx nvidia-580xx-settings libglvnd lib32-nvidia-580xx-utils lib32-opencl-nvidia-580xx egl-wayland
fi
;;
"nvidia-470xx-dkms")
# NOTE:
# Installing these drivers on a iMac14.2 with a GTX 750M resulted in sddm launching and xorg working but,
# plain tty doesnt render anymore.
if [[ "${legacyMode}" == "true" ]]; then
checkAurHelper
aurHelperInstall "nvidia-470xx-dkms nvidia-470xx-utils opencl-nvidia-470xx nvidia-470xx-settings libglvnd lib32-nvidia-470xx-utils lib32-opencl-nvidia-470xx egl-wayland"
else
checkChaoticAur
sudo pacman -S --needed --noconfirm nvidia-470xx-dkms nvidia-470xx-utils opencl-nvidia-470xx nvidia-470xx-settings libglvnd lib32-nvidia-470xx-utils lib32-opencl-nvidia-470xx egl-wayland
fi
;;
"nvidia-390xx-dkms")
if [[ "${legacyMode}" == "true" ]]; then
checkAurHelper
aurHelperInstall "nvidia-390xx-dkms nvidia-390xx-utils opencl-nvidia-390xx nvidia-390xx-settings libglvnd lib32-nvidia-390xx-utils lib32-opencl-nvidia-390xx egl-wayland"
else
checkChaoticAur
sudo pacman -S --needed --noconfirm nvidia-390xx-dkms nvidia-390xx-utils opencl-nvidia-390xx nvidia-390xx-settings libglvnd lib32-nvidia-390xx-utils lib32-opencl-nvidia-390xx egl-wayland
fi
;;
"nvidia-340xx-dkms")
# NOTE:
# The nvidia-340xx-settings fails to install because its denied access to /usr/local/share/man/ ...
# Also testing this driver in a vm resulted in alacritty not starting anymore. (╯°□°)╯︵ ┻━┻
if [[ "${legacyMode}" == "true" ]]; then
checkAurHelper
aurHelperInstall "nvidia-340xx-dkms nvidia-340xx-utils opencl-nvidia-340xx libglvnd lib32-nvidia-340xx-utils lib32-opencl-nvidia-340xx egl-wayland"
else
# NOTE:
# The chaotic aur does not have the lib32-nvidia-340xx-utils and lib32-opencl-nvidia-340xx packages yet.
checkChaoticAur
sudo pacman -S --needed --noconfirm nvidia-340xx-dkms nvidia-340xx-utils opencl-nvidia-340xx libglvnd egl-wayland
fi
;;
*)
logMessage "error" "No package provided for installation."
;;
esac
logMessage "info" "Successfully installed ${gpuDriver} and dependencies."
}
configureMkinitcpio() {
# This was just pure insanity to impliment with the intent
# of not breaking previous configurations. (But it works :3)
# This is for adding the nvidia modules to the /etc/mkinitcpio.conf file.
# Firstly it creates a backup of the original file.
# Then it removes any lines that are commented out and contain nothing. (Not necessary but pretty)
# Then removes any previously added nvidia modules, this could fix previously wrong configurations.
# Ensures the () dont have any spaces at the beginning and at the end.
# Then the modules get added in the correct formatting and order without deleting previous modules not related to nvidia.
# At the end the kms hook gets removed, which is a recommeded step because it disables any other non-nvidia gpu.
local config="/etc/mkinitcpio.conf"
backupConfig "${config}"
logMessage "info" "Configuring ${config}..."
# Remove any lines that are commented out and contain nothing
logMessage "info" "Cleaning up ${config}..."
sudo sed -i '/^#/d;/^$/d' "${config}"
# Remove any occurrences of nvidia-related modules in case some already exist.
# We dont want double arguments.
sudo sed -i 's/\b\(nvidia\|nvidia_modeset\|nvidia_uvm\|nvidia_drm\)\b//g' "${config}"
# Ensure exactly one space between words and no space after '(' or before ')'
logMessage "info" "Cleaning up brackets..."
sudo sed -i 's/ ( /(/g; s/ )/)/g; s/( */(/; s/ *)/)/; s/ \+/ /g' "${config}"
# Determine if either installing nvidia-340xx-dkms or later
logMessage "info" "Adding NVIDIA modules..."
if [[ ${gpuDriver} == "nvidia-340xx-dkms" ]]; then
# Add nvidia nvidia_uvm at the end of HOOKS=()
sudo sed -i 's/^MODULES=(\([^)]*\))/MODULES=(\1 nvidia nvidia_uvm)/' "${config}"
else
# Add nvidia nvidia_modeset nvidia_uvm nvidia_drm at the end of HOOKS=()
sudo sed -i 's/^MODULES=(\([^)]*\))/MODULES=(\1 nvidia nvidia_modeset nvidia_uvm nvidia_drm)/' "${config}"
fi
# Ensure exactly one space between words and no space after '(' or before ')'
logMessage "info" "Cleaning up brackets..."
sudo sed -i 's/ ( /(/g; s/ )/)/g; s/( */(/; s/ *)/)/; s/ \+/ /g' "${config}"
# Remove kms from HOOKS=()
logMessage "info" "Removing kms hook..."
sudo sed -i 's/\bkms \b//g' "${config}"
logMessage "info" "Configured ${config}."
}
configureModprobe() {
# "options nvidia_drm modeset=1 fbdev=1" straight from Hyprland Wiki.
# This isnt needed but still good for compatibility.
local config="/etc/modprobe.d/nvidia.conf"
backupConfig "${config}"
logMessage "info" "Configuring ${config}..."
echo "options nvidia_drm modeset=1 fbdev=1" | sudo tee "${config}" >/dev/null
logMessage "info" "Configured ${config}."
}
configureGrubDefault() {
# Function to add "nvidia_drm.modeset=1" to /etc/default/grub.
# The weird sed syntax ensures that the argument only gets added
# and not replacing the line, keeping previous configuration safe.
local config="/etc/default/grub"
backupConfig "${config}"
logMessage "info" "Configuring ${config}..."
# Remove nvidia_drm.modeset=1 from GRUB_CMDLINE_LINUX in case it exists.
# We dont want double arguments.
sudo sed -i 's/nvidia_drm\.modeset=1//g' "${config}"
# Add nvidia_drm.modeset=1 to GRUB_CMDLINE_LINUX
logMessage "info" "Adding NVIDIA modeset to ${config}..."
sudo sed -i '/GRUB_CMDLINE_LINUX_DEFAULT=/!b;/nvidia_drm.modeset=1/!s/\(GRUB_CMDLINE_LINUX_DEFAULT="[^"]*\)/\1 nvidia_drm.modeset=1/' "${config}"
logMessage "info" "Configured ${config}."
}
regenerateInitramfs() {
# Regenerates the initramfs to load the nvidia modules.
# Prepare for high CPU usage.
logMessage "info" "Regenerating initramfs... (this may take a while)"
sudo mkinitcpio -P || logMessage "error" "Failed to regenerate the initramfs."
logMessage "info" "Regernerated initramfs."
}
updateGrubConfig() {
# Updates the grub config at /boot/grub/grub.cfg
# After /etc/default/grub was changed
local config="/boot/grub/grub.cfg"
backupConfig "${config}"
logMessage "info" "Configuring ${config}..."
sudo grub-mkconfig -o "${config}" || logMessage "error" "Failed to update ${config}."
logMessage "info" "Configured ${config}"
}
confirmReboot() {
# Asks the user to reboot to apply changes.
echo -e ""
echo -e "${green}Action complete.${reset}"
read -rp "Would you like to reboot now? (y/N): " rebootNow
case "${rebootNow}" in
[yY][eE][sS] | [yY])
sudo reboot now
;;
*)
logMessage "info" "Please reboot your system later to apply changes."
echo -e ""
echo -e "\t${green}Press any button to return${reset}"
read -rsn1 option
case "${option}" in
*) ;;
esac
;;
esac
}
### UNINSTALLATION STEPS ###
confirmUninstallation() {
# Same as confirmInstallation
clear
echo -e "\t┌──────────────────────────────────────────────────┐"
echo -e "\t│ / \ │"
echo -e "\t│ / | \ This script will ${red}uninstall${reset} NVIDIA │"
echo -e "\t│ / # \ drivers and modify system │"
echo -e "\t│ /_______\ configurations. │"
echo -e "\t│ │"
echo -e "\t│ [!] Proceed with caution! │"
echo -e "\t└──────────────────────────────────────────────────┘"
echo -e ""
read -rp "Do you want to uninstall ${installedDriver}? (y/N): " confirm
case "${confirm}" in
[yY][eE][sS] | [yY])
echo -e "${green}Uninstalling ${installedDriver}...${reset}"
uninstallationSteps
;;
*)
exitScript "Uninstallation cancelled."
;;
esac
}
uninstallationSteps() {
# Just a simple function handling each steps because
# handling it everywere else looked ugly, Part 2.
# Step 1
removeNvidiaPackages
# Step 2
removeMkinitcpio
# Step 3
removeModprobe
# Step 4
removeGrubDefault
# Step 5
regenerateInitramfs
# Step 6
updateGrubConfig
# Step 7
confirmReboot
}
removeNvidiaPackages() {
# Uninstalls the nvidia driver.
# Only removes the main nvidia package because
# if a user doesnt have any of the other ones the uninstallation would fail...
# Compatibility against usability. :/
logMessage "info" "Uninstalling ${installedDriver}..."
case "${installedDriver}" in
"nvidia")
sudo pacman -R --noconfirm nvidia
;;
"nvidia-open-dkms")
sudo pacman -R --noconfirm nvidia-open-dkms
;;
"nvidia-580xx-dkms")
if [[ "${legacyMode}" == "true" ]]; then
checkAurHelper
aurHelperUninstall "nvidia-580xx-dkms"
else
checkChaoticAur
sudo pacman -R --noconfirm nvidia-580xx-dkms
fi
;;
"nvidia-470xx-dkms")
if [[ "${legacyMode}" == "true" ]]; then
checkAurHelper
aurHelperUninstall "nvidia-470xx-dkms"
else
checkChaoticAur
sudo pacman -R --noconfirm nvidia-470xx-dkms
fi
;;
"nvidia-390xx-dkms")
if [[ "${legacyMode}" == "true" ]]; then
checkAurHelper
aurHelperUninstall "nvidia-390xx-dkms"
else
checkChaoticAur
sudo pacman -R --noconfirm nvidia-390xx-dkms
fi
;;
"nvidia-340xx-dkms")
if [[ "${legacyMode}" == "true" ]]; then
checkAurHelper
aurHelperUninstall "nvidia-340xx-dkms"
else
checkChaoticAur
sudo pacman -R --noconfirm nvidia-340xx-dkms
fi
;;
*)
logMessage "error" "No package provided for uninstallation."
;;
esac
logMessage "info" "Successfully uninstalled ${installedDriver}."
}
removeMkinitcpio() {
# Same as the configureMkinitcpio() function but without adding the nvidia modules.
# Also adds back the kms hook.
local config="/etc/mkinitcpio.conf"
backupConfig "${config}"
logMessage "info" "Configuring ${config}..."
# Remove any lines that are commented out and contain nothing
logMessage "info" "Cleaning up ${config} structure..."
sudo sed -i '/^#/d;/^$/d' "${config}"
# Remove any occurrences of nvidia-related modules
logMessage "info" "Removing NVIDIA modules..."
sudo sed -i 's/\b\(nvidia\|nvidia_modeset\|nvidia_uvm\|nvidia_drm\)\b//g' "${config}"
# Ensure exactly one space between words and no space after '(' or before ')'
sudo sed -i 's/ ( /(/g; s/ )/)/g; s/( */(/; s/ *)/)/; s/ \+/ /g' "${config}"
# Remove kms from HOOKS=() in case it already exists.
# We dont want double arguments.
sudo sed -i 's/\bkms \b//g' "${config}"
# Add kms to HOOKS=()
logMessage "info" "Adding kms hook..."
sudo sed -i 's/modconf/& kms/' "${config}"
logMessage "info" "Configured ${config}."
}
removeModprobe() {
# Creates a backup of the /etc/modprobe.d/nvidia.conf file and deletes the original one.
local config="/etc/modprobe.d/nvidia.conf"
backupConfig "${config}"
logMessage "info" "Deleting ${config}..."
# Delete configuration file
sudo rm -f "${config}" || logMessage "warning" "Failed to delete NVIDIA modprobe file."
logMessage "info" "Deleted ${config}."
}
removeGrubDefault() {
# Creates a backup of the /etc/default/grub file
# Removes nvidia_drm.modeset=1 from GRUB_CMDLINE_LINUX
local config="/etc/default/grub"
backupConfig "${config}"
logMessage "info" "Configuring ${config}..."
# Remove nvidia_drm.modeset=1 from GRUB_CMDLINE_LINUX
sudo sed -i 's/nvidia_drm\.modeset=1//g' "${config}"
logMessage "info" "Configured ${config}."
}
exitScript() {
local message="$1"
echo -e ""
echo -e "${red}${message}${reset}"
exit 0
}
printHelp() {
echo -e "usage: $(basename "$0") [...]"
echo -e "arguments:"
echo -e "\t -l | --legacy"
echo -e "\t -h | --help"
echo -e "\t -v | --version"
echo -e ""
exit 0
}
printVersion() {
echo -e " $(basename "$0") v${scriptVersion} - GNU bash, version 5.3"
echo -e " Copyright (C) 2025-present Justus0405"
echo -e ""
exit 0
}
### PROGRAM START ###
# Step 1: Set up trap for SIGINT (CTRL+C)
trap 'exitScript "Aborted!"' SIGINT
# Step 2: Get the launch arguments
getArguments "$@"
# Step 3: Check if running as sudo
checkSudo
# Step 4: Identify NVIDIA card, if that fails prompt the user to select a driver
checkNvidia
# Step 5: Detect if a driver is already installed, needed for uninstallation handling.
checkInstalledDriver