fork/home-manager

git clone https://git.julia.blue/fork/home-manager

Robert HelgessonMerge PR #421575790441

master
2.8 KiB102 linesraw
1{ config, lib, pkgs, ... }:
2
3with lib;
4
5let
6
7  cfg = config.programs.go;
8
9in {
10  meta.maintainers = [ maintainers.rvolosatovs ];
11
12  options = {
13    programs.go = {
14      enable = mkEnableOption "Go";
15
16      package = mkOption {
17        type = types.package;
18        default = pkgs.go;
19        defaultText = literalExpression "pkgs.go";
20        description = "The Go package to use.";
21      };
22
23      packages = mkOption {
24        type = with types; attrsOf path;
25        default = { };
26        example = literalExpression ''
27          {
28            "golang.org/x/text" = builtins.fetchGit "https://go.googlesource.com/text";
29            "golang.org/x/time" = builtins.fetchGit "https://go.googlesource.com/time";
30          }
31        '';
32        description = "Packages to add to GOPATH.";
33      };
34
35      goPath = mkOption {
36        type = with types; nullOr str;
37        default = null;
38        example = "go";
39        description = ''
40          Primary {env}`GOPATH` relative to
41          {env}`HOME`. It will be exported first and therefore
42          used by default by the Go tooling.
43        '';
44      };
45
46      extraGoPaths = mkOption {
47        type = types.listOf types.str;
48        default = [ ];
49        example = [ "extraGoPath1" "extraGoPath2" ];
50        description = ''
51          Extra {env}`GOPATH`s relative to {env}`HOME` appended
52          after [](#opt-programs.go.goPath), if that option is set.
53        '';
54      };
55
56      goBin = mkOption {
57        type = with types; nullOr str;
58        default = null;
59        example = ".local/bin.go";
60        description = "GOBIN relative to HOME";
61      };
62
63      goPrivate = mkOption {
64        type = with types; listOf str;
65        default = [ ];
66        example = [ "*.corp.example.com" "rsc.io/private" ];
67        description = ''
68          The {env}`GOPRIVATE` environment variable controls
69          which modules the go command considers to be private (not
70          available publicly) and should therefore not use the proxy
71          or checksum database.
72        '';
73      };
74    };
75  };
76
77  config = mkIf cfg.enable (mkMerge [
78    {
79      home.packages = [ cfg.package ];
80
81      home.file = let
82        goPath = if cfg.goPath != null then cfg.goPath else "go";
83        mkSrc = n: v: { "${goPath}/src/${n}".source = v; };
84      in foldl' (a: b: a // b) { } (mapAttrsToList mkSrc cfg.packages);
85    }
86
87    (mkIf (cfg.goPath != null) {
88      home.sessionVariables.GOPATH = concatStringsSep ":" (map builtins.toPath
89        (map (path: "${config.home.homeDirectory}/${path}")
90          ([ cfg.goPath ] ++ cfg.extraGoPaths)));
91    })
92
93    (mkIf (cfg.goBin != null) {
94      home.sessionVariables.GOBIN =
95        builtins.toPath "${config.home.homeDirectory}/${cfg.goBin}";
96    })
97
98    (mkIf (cfg.goPrivate != [ ]) {
99      home.sessionVariables.GOPRIVATE = concatStringsSep "," cfg.goPrivate;
100    })
101  ]);
102}