From 0f8b5fa88d5871ecffd320c1a8fe804c1117bced Mon Sep 17 00:00:00 2001 From: Aoran Zeng Date: Tue, 24 Feb 2026 17:31:09 +0800 Subject: [PATCH] =?UTF-8?q?=E8=BF=90=E8=A1=8C=E6=97=B6=E6=A3=80=E6=B5=8B?= =?UTF-8?q?=20`Nix`=20=E5=AE=89=E8=A3=85=E6=96=B9=E5=BC=8F=E7=84=B6?= =?UTF-8?q?=E5=90=8E=E5=86=8D=E7=A1=AE=E5=AE=9A=20scope=20[GitHub=20#337]?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/recipe/ware/Nix.c | 49 +++++++++++++++++++++++++++++++++++++++---- 1 file changed, 45 insertions(+), 4 deletions(-) diff --git a/src/recipe/ware/Nix.c b/src/recipe/ware/Nix.c index 5f71359..d9076a8 100644 --- a/src/recipe/ware/Nix.c +++ b/src/recipe/ware/Nix.c @@ -4,13 +4,31 @@ def_target(wr_nix, "nix"); +/** + * 运行时检查到底是哪种安装方式 + * + * https://github.com/RubyMetric/chsrc/issues/337 + */ +bool +wr_nix_is_multi_user_installation_mode () +{ + if (xy_file_exist ("/nix/var/nix/daemon-socket/socket")) + { + return true; + } + else + { + return false; + } +} + void wr_nix_prelude () { chef_prep_this (wr_nix, s); chef_set_recipe_created_on (this, "2023-09-26"); - chef_set_recipe_last_updated (this, "2025-08-09"); + chef_set_recipe_last_updated (this, "2026-02-24"); chef_set_sources_last_updated (this, "2025-07-13"); chef_set_chef (this, NULL); @@ -19,8 +37,16 @@ wr_nix_prelude () chef_set_scope_cap (this, ProjectScope, ScopeCap_Unable); chef_set_scope_cap (this, UserScope, ScopeCap_Able_And_Implemented); - chef_set_scope_cap (this, SystemScope, ScopeCap_Unable); - chef_set_default_scope (this, UserScope); + chef_set_scope_cap (this, SystemScope, ScopeCap_Able_And_Implemented); + + if (wr_nix_is_multi_user_installation_mode ()) + { + chef_set_default_scope (this, SystemScope); + } + else + { + chef_set_default_scope (this, UserScope); + } chef_deny_english(this); chef_deny_user_define(this); @@ -48,6 +74,7 @@ wr_nix_check_cmd () * @consult * 1. https://help.mirrors.cernet.edu.cn/nix-channels/ * 2. https://gitee.com/RubyMetric/chsrc/issues/I83894 + * 3. https://github.com/RubyMetric/chsrc/issues/337 */ void wr_nix_setsrc (char *option) @@ -56,11 +83,25 @@ wr_nix_setsrc (char *option) chsrc_use_this_source (wr_nix); + char *user_scope_nix_config = "~/.config/nix/nix.conf"; + char *system_scope_nix_config = "/etc/nix/nix.conf"; + + char *config_file = NULL; + + if (wr_nix_is_multi_user_installation_mode ()) + { + config_file = system_scope_nix_config; + } + else + { + config_file = user_scope_nix_config; + } + char *cmd = xy_strcat (3, "nix-channel --add ", source.url, "nixpkgs-unstable nixpkgs"); chsrc_run (cmd, RunOpt_Default); char *w = xy_strcat (3, "substituters = ", source.url, "store https://cache.nixos.org/"); - chsrc_append_to_file (w, "~/.config/nix/nix.conf"); + chsrc_append_to_file (w, config_file); chsrc_run ("nix-channel --update", RunOpt_Default);