Skip to content

Commit 0f8b5fa

Browse files
committed
运行时检测 Nix 安装方式然后再确定 scope
[GitHub #337]
1 parent 7efad63 commit 0f8b5fa

File tree

1 file changed

+45
-4
lines changed

1 file changed

+45
-4
lines changed

src/recipe/ware/Nix.c

Lines changed: 45 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,31 @@
44

55
def_target(wr_nix, "nix");
66

7+
/**
8+
* 运行时检查到底是哪种安装方式
9+
*
10+
* https://github.com/RubyMetric/chsrc/issues/337
11+
*/
12+
bool
13+
wr_nix_is_multi_user_installation_mode ()
14+
{
15+
if (xy_file_exist ("/nix/var/nix/daemon-socket/socket"))
16+
{
17+
return true;
18+
}
19+
else
20+
{
21+
return false;
22+
}
23+
}
24+
725
void
826
wr_nix_prelude ()
927
{
1028
chef_prep_this (wr_nix, s);
1129

1230
chef_set_recipe_created_on (this, "2023-09-26");
13-
chef_set_recipe_last_updated (this, "2025-08-09");
31+
chef_set_recipe_last_updated (this, "2026-02-24");
1432
chef_set_sources_last_updated (this, "2025-07-13");
1533

1634
chef_set_chef (this, NULL);
@@ -19,8 +37,16 @@ wr_nix_prelude ()
1937

2038
chef_set_scope_cap (this, ProjectScope, ScopeCap_Unable);
2139
chef_set_scope_cap (this, UserScope, ScopeCap_Able_And_Implemented);
22-
chef_set_scope_cap (this, SystemScope, ScopeCap_Unable);
23-
chef_set_default_scope (this, UserScope);
40+
chef_set_scope_cap (this, SystemScope, ScopeCap_Able_And_Implemented);
41+
42+
if (wr_nix_is_multi_user_installation_mode ())
43+
{
44+
chef_set_default_scope (this, SystemScope);
45+
}
46+
else
47+
{
48+
chef_set_default_scope (this, UserScope);
49+
}
2450

2551
chef_deny_english(this);
2652
chef_deny_user_define(this);
@@ -48,6 +74,7 @@ wr_nix_check_cmd ()
4874
* @consult
4975
* 1. https://help.mirrors.cernet.edu.cn/nix-channels/
5076
* 2. https://gitee.com/RubyMetric/chsrc/issues/I83894
77+
* 3. https://github.com/RubyMetric/chsrc/issues/337
5178
*/
5279
void
5380
wr_nix_setsrc (char *option)
@@ -56,11 +83,25 @@ wr_nix_setsrc (char *option)
5683

5784
chsrc_use_this_source (wr_nix);
5885

86+
char *user_scope_nix_config = "~/.config/nix/nix.conf";
87+
char *system_scope_nix_config = "/etc/nix/nix.conf";
88+
89+
char *config_file = NULL;
90+
91+
if (wr_nix_is_multi_user_installation_mode ())
92+
{
93+
config_file = system_scope_nix_config;
94+
}
95+
else
96+
{
97+
config_file = user_scope_nix_config;
98+
}
99+
59100
char *cmd = xy_strcat (3, "nix-channel --add ", source.url, "nixpkgs-unstable nixpkgs");
60101
chsrc_run (cmd, RunOpt_Default);
61102

62103
char *w = xy_strcat (3, "substituters = ", source.url, "store https://cache.nixos.org/");
63-
chsrc_append_to_file (w, "~/.config/nix/nix.conf");
104+
chsrc_append_to_file (w, config_file);
64105

65106
chsrc_run ("nix-channel --update", RunOpt_Default);
66107

0 commit comments

Comments
 (0)