You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何编写flake.nix实现直接执行nix develop替代指定仓库命令?

问题解决方法

错误原因分析

  • 第一个错误:cosmos_inform_system.devShells是按系统架构分组的嵌套结构,直接访问shells.cosmos-shell会找不到属性——必须先获取当前系统对应的devShells子集,再访问目标shell。
  • 第二个错误:flake-utils.lib.eachDefaultSystem会自动将闭包返回的属性挂载到对应系统的输出下,无需手动指定devShells.${system},之前的写法破坏了Flake输出规范,导致报错“不是 derivation”。

修正后的flake.nix

{
  inputs = {
    nixpkgs.url = "github:nixos/nixpkgs/nixos-unstable";
    cosmos_inform_system.url = "github:informalsystems/cosmos.nix";
    flake-utils.url = "github:numtide/flake-utils";
  };

  outputs = { self, nixpkgs, cosmos_inform_system, flake-utils, ... }:
    flake-utils.lib.eachDefaultSystem (system:
      let
        # 获取当前系统对应的devShells集合
        systemShells = cosmos_inform_system.devShells.${system};
      in {
        # 为当前系统设置默认devShell
        devShells.default = systemShells.cosmos-shell;
      });
}

验证命令

执行以下命令即可直接加载目标shell:

# 已启用Nix实验性功能的情况
nix develop

# 未启用实验性功能的情况
nix develop --extra-experimental-features nix-command --extra-experimental-features flakes

内容的提问来源于stack exchange,提问作者Pierre-olivier Gendraud

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.04 04:55:28