aboutsummaryrefslogtreecommitdiff
path: root/flake.nix
blob: 35c02318083581c4deedb6648e7718181cef3af2 (plain)
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
{
  description = "Cynerd's shell configuration";

  outputs = { self, flake-utils, nixpkgs }: 
  let

    loadrc = dir: ''
      for sh in ${dir}/*; do
          [ -r "$sh" ] && . "$sh"
      done
      '';
    commonrc = loadrc ./shellrc.d;
    desktoprc = loadrc ./shellrc-desktop.d;
    bashrc = loadrc ./bashrc.d;
    zshrc = loadrc ./zshrc.d;

    packages = pkgs: rec {
      shellrc-generic = pkgs.stdenvNoCC.mkDerivation {
        name = "shellrc-profile";
        src = ./.;
        installPhase = ''
          mkdir -p "$out/etc/shellrc"
          cp -r ./shellrc.d/. "$out/etc/shellrc/"
        '';
      };
      shellrc-bashrc = pkgs.stdenvNoCC.mkDerivation {
        name = "shellrc-profile-bash";
        src = ./.;
        shellrc = shellrc-generic;
        installPhase = ''
          mkdir -p "$out/etc/bashrc.d"
          cp -r ./bashrc.d/. "$out/etc/bashrc.d/"
          cat >"$out/etc/bashrc.d/shellrc" <<EOF
          for sh in $shellrc/etc/shellrc/*; do
            [ -r "\$sh" ] && . "\$sh"
          done
          EOF
        '';
      };
      shellrc-zshrc = pkgs.stdenvNoCC.mkDerivation {
        name = "shellrc-profile-zsh";
        src = ./.;
        shellrc = shellrc-generic;
        installPhase = ''
          mkdir -p "$out/etc/zshrc.d"
          cp -r ./zshrc.d/. "$out/etc/zshrc.d/"
          cat >"$out/etc/zshrc.d/shellrc" <<EOF
          for sh in $shellrc/etc/shellrc/*; do
            [ -r "\$sh" ] && . "\$sh"
          done
          EOF
        '';
      };
      shellrc-completion = pkgs.stdenvNoCC.mkDerivation {
        name = "shellrc-completion";
        src = ./.;
        nativeBuildInputs = [ pkgs.installShellFiles ];
        installPhase = ''
          for comp in bash-completion/*; do
            installShellCompletion --bash --name "''${comp##*/}.bash" "$comp"
          done
          for comp in zsh-completion/*; do
            installShellCompletion --zsh --name "''${comp##*/}" "$comp"
          done
        '';
      };
      shellrc = pkgs.symlinkJoin {
        name = "shellrc";
        paths = [ shellrc-bashrc shellrc-zshrc shellrc-completion ];
      };
      default = shellrc;
    };

  in {

    overlays = {
      shellrc = final: prev: packages prev;
      default = self.overlays.shellrc;
    };

    nixosModules = {
      shellrc = { config, lib, pkgs, ... }: with lib; {
        options = {
          programs.shellrc = {
            enable = mkOption {
              type = types.bool;
              default = true;
              description = "If shellrc should be enabled.";
            };
            desktop = mkOption {
              type = types.bool;
              default = false;
              description = "If shellrc's desktop specific files should be used.";
            };
          };
        };

        config = mkIf config.programs.shellrc.enable {

          environment.interactiveShellInit = commonrc + optionalString config.programs.shellrc.desktop desktoprc;

          programs.bash.interactiveShellInit = bashrc;
          programs.bash.promptInit = ""; # Disable default prompt as we have our own

          programs.zsh.interactiveShellInit = mkIf config.programs.zsh.enable zshrc;
          programs.zsh.promptInit = ""; # Disable default prompt as we have our own

          nixpkgs.overlays = [ self.overlays.shellrc ];
          environment.systemPackages = [ pkgs.shellrc-completion ];

        };
      };
      default = self.nixosModules.shellrc;
    };

  } // (flake-utils.lib.eachDefaultSystem (system: {
    packages = packages nixpkgs.legacyPackages.${system};
  }));

}