nix - 找出我在 nix 中使用的系统类型
问题描述
我想以在 NixOs (linux) 和 MacOs (darwin) 上工作的方式编写我的 nixos-configuration 和 home-manager-configuration 文件。
虽然有些东西在两个系统(例如 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;
}
推荐阅读
- python - 如何在树莓派上构建 FastAI docker 镜像?
- java - 如何更改用户输入的编辑文本的颜色?
- ios - 如何为用户设置更改密码按钮?
- javascript - 重复异步函数直到为真
- javascript - 如何重写这个 batch.delete 函数来为实时数据库工作?
- c# - XmlSerializer:反序列化递归对象图
- angular - 动画在 Angular 项目中不起作用
- spring - 如何使用 Spring Framework 实现多点模式
- xamarin.forms - 无法在 iOS 上的 Xamarin UITest 中使用 ScrollDownTo() 向下滚动到 ScrollView 中不可见的控件
- node.js - 出现“无法在模块外使用 import 语句”错误并且不想升级 Node