nixos下golang+goland开发环境

nixos 下开发环境可以独立配置,golang的环境相对来说比较简单。 在现有的go项目目录下,或者打算储存go项目的目录里面创建一个flake.nix 文件内容如下。

rust和flutter nodejs等开发环境 本站也有,请自行检索

本文已经更新 详情看文末

nix develop 环境 (推荐)

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
{
  description = "golang ";
  inputs = {
    #nixpkgs.url = "github:NixOS/nixpkgs/23.11";
    nixpkgs.url = "https://mirrors.ustc.edu.cn/nix-channels/nixpkgs-unstable/nixexprs.tar.xz";
    flake-utils.url = "github:numtide/flake-utils";
  };
  outputs = { self, nixpkgs, flake-utils }:
    flake-utils.lib.eachDefaultSystem (system:
      {
        devShell =
          with pkgs; mkShell rec {
            GOPROXY = "https://goproxy.cn,direct";
            buildInputs = [
              go_1_22
              gopls
              gcc
              upx
              jetbrains.goland
              fish
              #ninja cmake glibc # 如果需要用最好自行装到 home.packages 装这里可能有问题
            ];
          };
      });
}

homemanager 环境(不建议)

1
2
3
4
5
6
7
8
9
{ config, lib, pkgs, ... }:
{
  home.packages = with pkgs;[ 
    go_1_22
    gopls
    gcc
    upx
  ];
}

nix run方式(2024-11-4更新)

1
 nix run .#dev --show-trace
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
{
{
  description = "A Nix-flake-based golang development environment";

  inputs = {
    #nixpkgs.url = "github:NixOS/nixpkgs/23.11";
    #nixpkgs.url = "https://mirrors.ustc.edu.cn/nix-channels/nixpkgs-unstable/nixexprs.tar.xz";
    nixpkgs.url = "https://mirrors.ustc.edu.cn/nix-channels/nixos-24.05/nixexprs.tar.xz";
  };

  outputs = { self , nixpkgs ,... }: let
    # system should match the system you are running on
    system = "x86_64-linux";
    # system = "x86_64-darwin";
  in {
    packages."${system}".dev = let
      pkgs = import nixpkgs {
        inherit system;
        config.allowUnfree = true;
      };
      packages = with pkgs; [
          go_1_23
          gopls
          gcc
          upx
          jetbrains.goland # allowUnfree
          nushell
      ];
    in pkgs.runCommand "dev-shell" {
      # Dependencies that should exist in the runtime environment
      buildInputs = packages;
      # Dependencies that should only exist in the build environment
      nativeBuildInputs = [ pkgs.makeWrapper ];
    } ''
      mkdir -p $out/bin/
      ln -s ${pkgs.nushell}/bin/nu $out/bin/dev-shell
      wrapProgram $out/bin/dev-shell --set GOPROXY https://goproxy.cn,direct
      wrapProgram $out/bin/dev-shell --prefix PATH : ${pkgs.lib.makeBinPath packages}
    '';
  };
}
Licensed under CC BY-NC-SA 4.0
comments powered by Disqus
使用 Hugo 构建
主题 StackJimmy 设计