11#! /bin/bash
22# # Author: SuperManito
3- # # Modified: 2025-01-22
3+ # # Modified: 2025-02-20
44# # License: MIT
55# # GitHub: https://github.com/SuperManito/LinuxMirrors
66# # Website: https://linuxmirrors.cn
@@ -176,6 +176,7 @@ SYSTEM_OPENSUSE="openSUSE"
176176SYSTEM_ARCH=" Arch"
177177SYSTEM_ALPINE=" Alpine"
178178SYSTEM_GENTOO=" Gentoo"
179+ SYSTEM_NIXOS=" NixOS"
179180
180181# # 定义系统版本文件
181182File_LinuxRelease=/etc/os-release
@@ -213,13 +214,16 @@ File_GentooMakeConf=/etc/portage/make.conf
213214File_GentooMakeConfBackup=/etc/portage/make.conf.bak
214215File_GentooReposConf=/etc/portage/repos.conf/gentoo.conf
215216File_GentooReposConfBackup=/etc/portage/repos.conf/gentoo.conf.bak
217+ File_NixConf=/etc/nix/nix.conf
218+ File_NixConfBackup=/etc/nix/nix.conf.bak
216219Dir_GentooReposConf=/etc/portage/repos.conf
217220Dir_DebianExtendSource=/etc/apt/sources.list.d
218221Dir_DebianExtendSourceBackup=/etc/apt/sources.list.d.bak
219222Dir_YumRepos=/etc/yum.repos.d
220223Dir_YumReposBackup=/etc/yum.repos.d.bak
221224Dir_ZYppRepos=/etc/zypp/repos.d
222225Dir_ZYppReposBackup=/etc/zypp/repos.d.bak
226+ Dir_NixConfig=/etc/nix
223227
224228# # 定义颜色变量
225229RED=' \033[31m'
@@ -660,6 +664,8 @@ function collect_system_info() {
660664 SYSTEM_FACTIONS=" ${SYSTEM_GENTOO} "
661665 elif [[ " ${SYSTEM_NAME} " == * " openSUSE" * ]]; then
662666 SYSTEM_FACTIONS=" ${SYSTEM_OPENSUSE} "
667+ elif [[ " ${SYSTEM_NAME} " == * " NixOS" * ]]; then
668+ SYSTEM_FACTIONS=" ${SYSTEM_NIXOS} "
663669 else
664670 output_error " 当前操作系统不在本脚本的支持范围内,请前往官网查看支持列表!"
665671 fi
@@ -753,7 +759,7 @@ function collect_system_info() {
753759 ;;
754760 esac
755761 ;;
756- " ${SYSTEM_KALI} " | " ${SYSTEM_DEEPIN} " | " ${SYSTEM_ZORIN} " | " ${SYSTEM_ARCH} " | " ${SYSTEM_ALPINE} " | " ${SYSTEM_GENTOO} " | " ${SYSTEM_OPENKYLIN} " )
762+ " ${SYSTEM_KALI} " | " ${SYSTEM_DEEPIN} " | " ${SYSTEM_ZORIN} " | " ${SYSTEM_ARCH} " | " ${SYSTEM_ALPINE} " | " ${SYSTEM_GENTOO} " | " ${SYSTEM_OPENKYLIN} " | " ${SYSTEM_NIXOS} " )
757763 # 理论全部支持或不作判断
758764 ;;
759765 * )
@@ -860,6 +866,9 @@ function collect_system_info() {
860866 SOURCE_BRANCH=" ${SOURCE_BRANCH// / -} "
861867 fi
862868 ;;
869+ " ${SYSTEM_NIXOS} " )
870+ SOURCE_BRANCH=" nix-channels"
871+ ;;
863872 esac
864873 fi
865874 # # 定义软件源更新文字
@@ -876,6 +885,9 @@ function collect_system_info() {
876885 " ${SYSTEM_ARCH} " | " ${SYSTEM_GENTOO} " )
877886 SYNC_MIRROR_TEXT=" 同步软件源"
878887 ;;
888+ " ${SYSTEM_NIXOS} " )
889+ SYNC_MIRROR_TEXT=" 更新二进制缓存与频道源"
890+ ;;
879891 esac
880892 # # 判断是否可以使用高级交互式选择器
881893 CAN_USE_ADVANCED_INTERACTIVE_SELECTION=" false"
@@ -1326,6 +1338,11 @@ function backup_original_mirrors() {
13261338 [ -d " ${Dir_GentooReposConf} " ] || mkdir -p " ${Dir_GentooReposConf} "
13271339 backup_file $File_GentooReposConf $File_GentooReposConfBackup " gentoo.conf"
13281340 ;;
1341+ " ${SYSTEM_NIXOS} " )
1342+ [ ! -d $Dir_NixConfig ] && mkdir -p $Dir_NixConfig
1343+ # /etc/nix/nix.conf
1344+ backup_file $File_NixConf $File_NixConfBackup " nix.conf"
1345+ ;;
13291346 esac
13301347 fi
13311348}
@@ -1523,6 +1540,9 @@ function change_mirrors_main() {
15231540 diff_file $File_GentooMakeConfBackup $File_GentooMakeConf
15241541 diff_file $File_GentooReposConfBackup $File_GentooReposConf
15251542 ;;
1543+ " ${SYSTEM_NIXOS} " )
1544+ diff_file $File_NixConfBackup $File_NixConf
1545+ ;;
15261546 esac
15271547 fi
15281548 }
@@ -1559,6 +1579,9 @@ function change_mirrors_main() {
15591579 " ${SYSTEM_OPENKYLIN} " )
15601580 change_mirrors_openKylin
15611581 ;;
1582+ " ${SYSTEM_NIXOS} " )
1583+ change_mirrors_NixOS
1584+ ;;
15621585 esac
15631586 # # 比较差异
15641587 if [[ " ${PRINT_DIFF} " == " true" ]]; then
@@ -1586,6 +1609,10 @@ function change_mirrors_main() {
15861609 " ${SYSTEM_GENTOO} " )
15871610 emerge --sync --quiet
15881611 ;;
1612+ " ${SYSTEM_NIXOS} " )
1613+ nix-store --verify
1614+ nix-channel --update
1615+ ;;
15891616 esac
15901617 if [ $? -eq 0 ]; then
15911618 echo -e " \n$SUCCESS 软件源更换完毕"
@@ -1679,6 +1706,9 @@ function upgrade_software() {
16791706 " ${SYSTEM_GENTOO} " )
16801707 emerge --update --deep --with-bdeps=y --ask=n @world
16811708 ;;
1709+ " ${SYSTEM_NIXOS} " )
1710+ nixos-rebuild switch
1711+ ;;
16821712 esac
16831713 if [[ " ${CLEAN_CACHE} " == " false" ]]; then
16841714 return
@@ -1703,6 +1733,9 @@ function upgrade_software() {
17031733 eclean-dist --deep > /dev/null 2>&1
17041734 eclean-packages --deep > /dev/null 2>&1
17051735 ;;
1736+ " ${SYSTEM_NIXOS} " )
1737+ nix-collect-garbage -d > /dev/null 2>&1
1738+ ;;
17061739 esac
17071740}
17081741
@@ -2301,6 +2334,23 @@ deb ${1} ${2}-updates ${3}
23012334$( gen_source " ${base_url} " " ${SYSTEM_VERSION_CODENAME} " " ${repository_sections} " ) " >> $File_DebianSourceList
23022335}
23032336
2337+ # # 更换 NixOS 发行版软件源
2338+ function change_mirrors_NixOS() {
2339+ local binary_cache_source channel_source
2340+ if [[ " ${USE_OFFICIAL_SOURCE} " == " true" ]]; then
2341+ binary_cache_source=" https://cache.nixos.org/"
2342+ channel_source=" https://nixos.org/channels"
2343+ else
2344+ binary_cache_source=" ${WEB_PROTOCOL} ://${SOURCE} /${SOURCE_BRANCH} /store https://cache.nixos.org/"
2345+ channel_source=" ${WEB_PROTOCOL} ://${SOURCE} /${SOURCE_BRANCH} "
2346+ fi
2347+ # binary cache
2348+ sed -i " s|^substituters.*|substituters = ${binary_cache_source} |g" $File_NixConf
2349+ # channel
2350+ nix-channel --add " ${channel_source} /nixos-${SYSTEM_VERSION_NUMBER} " nixos
2351+ nix-channel --update > /dev/null 2>&1
2352+ }
2353+
23042354# # EPEL (Extra Packages for Enterprise Linux) 附加软件包 - 安装或更换软件源
23052355function change_mirrors_or_install_EPEL() {
23062356 if [[ " ${INSTALL_EPEL} " != " true" ]]; then
0 commit comments