如何解决找出我在 nix
我想编写我的 nixos-configuration 和 home-manager-configuration 文件,它们可以在 NixOs (linux) 和 MacOs (darwin) 上工作。
虽然有些东西在两个系统上的配置方式相同(例如 git),但其他只对其中一个系统有意义(比如 wayland-windowmanagers 只是 linux 上的一个东西)。
Nix 语言功能 if-else-statements,所以现在我需要的是一种方法来找出我使用的系统类型。
我所追求的是:
wayland.sway.enable = if (os == "MacOs") then false else true;
有没有办法找出我在 nix 中使用的系统?
解决方法
在 NixOS 模块中,您可以这样做:
{ config,lib,pkgs,... }:
{
wayland.sway.enable = if pkgs.stdenv.isLinux then true else false;
# or if you want to be more specific
wayland.sway.enable = if pkgs.system == "x86_64-linux" then true else false;
# or if you want to use the default otherwise
# this works for any option type
wayland.sway.enable = lib.mkIf pkgs.stdenv.isLinux true;
}
但是,我更喜欢将配置分解为模块,然后只imports
我需要的模块。
darwin-configuration.nix
{ ... }:
{
imports = [ ./common-configuration.nix ];
launchd.something.something = "...";
}
对于 NixOS:
configuration.nix
{ ... }:
{
imports = [ ./common-configuration.nix ];
wayland.sway.enable = true;
}
,
我(独立)找到了与@robert-hensing 相似的答案。所以这里是为了完整起见:
以下配置将在 MacOs 上安装“hello”,在其他系统上安装“tree”。
{ config,... }:
let
my_packages = if pkgs.system == "x86_64-darwin"
then [ pkgs.hello ]
else [ pkgs.tree ];
in
{
environment.systemPackages = my_packages;
}
您可以通过运行 nix repl '<nixpkgs>'
找到您的 pkgs.system 字符串,然后只需输入 system
。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。