返回 Skill 列表
extension
分类: 开发与工程无需 API Key

ellies-nix-environment

Nix环境模式用于Ellie的项目。当(1)系统上没有可用的工具或命令,或者(2)需要设置一个需要开发环境的新项目时使用。提供临时工具执行和基于flake与direnv的devShells模式。

person作者: jakexiaohubgithub

Ellie's Nix Environment

When a Tool Isn't Available

If a command fails because a tool isn't installed, use nix run to execute it:

nix run nixpkgs#<tool> -- <args>

Examples:

nix run nixpkgs#python3 -- script.py
nix run nixpkgs#jq -- '.field' file.json
nix run nixpkgs#ffmpeg -- -i input.mp4 output.gif
nix run nixpkgs#ripgrep -- "pattern" .

Do not suggest installing tools globally. Use nix run for one-off commands.

Setting Up a New Project

When a project needs a development environment, create a flake with direnv:

1. Create flake.nix

{
  description = "Project description";

  inputs = {
    nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable";
    flake-utils.url = "github:numtide/flake-utils";
  };

  outputs = { self, nixpkgs, flake-utils }:
    flake-utils.lib.eachDefaultSystem (system:
      let
        pkgs = nixpkgs.legacyPackages.${system};
      in
      {
        devShells.default = pkgs.mkShell {
          buildInputs = with pkgs; [
            # Add project dependencies here
          ];
        };
      });
}

2. Create .envrc

use flake .

3. Activate direnv

direnv allow

4. Add to .gitignore

.direnv/

Rust Projects

For Rust projects, use rust-overlay to get the toolchain:

{
  description = "Rust project";

  inputs = {
    nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable";
    flake-utils.url = "github:numtide/flake-utils";
    rust-overlay = {
      url = "github:oxalica/rust-overlay";
      inputs.nixpkgs.follows = "nixpkgs";
    };
  };

  outputs = { self, nixpkgs, flake-utils, rust-overlay }:
    flake-utils.lib.eachDefaultSystem (system:
      let
        overlays = [ (import rust-overlay) ];
        pkgs = import nixpkgs { inherit system overlays; };
        rust = pkgs.rust-bin.nightly.latest.default.override {
          extensions = [ "rust-src" "rust-analyzer" ];
        };
      in
      {
        devShells.default = pkgs.mkShell {
          buildInputs = [
            rust
          ];
        };
      });
}