summaryrefslogtreecommitdiff
path: root/2023/flake.nix
blob: ddf040832a739b2997bf5d8eefc611a841f82e72 (plain) (blame)
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
{
  description = "Environment for the Advent of Code 2023, solved in C++";

  inputs.devshell.url = "github:numtide/devshell";
  inputs.flake-utils.url = "github:numtide/flake-utils";

  inputs.flake-compat = {
    url = "github:edolstra/flake-compat";
    flake = false;
  };

  outputs = { self, flake-utils, devshell, nixpkgs, ... }:
    flake-utils.lib.eachDefaultSystem (system: {
      devShells.default = let
        pkgs = import nixpkgs {
          inherit system;
          overlays = [ devshell.overlays.default ];
        };
      in pkgs.devshell.mkShell {

        devshell = {
          motd = ''
            {bold}{3}🎄 Advent of Code 2023 🎄{reset}
            $(for CMD in g++ make gdb clangd ; do
              "$CMD" --version | head -n1
            done)
            $(type -p menu &>/dev/null && menu)
          '';
          interactive = {
            prompt.text = ''
              export PS1="\n> "
            '';
          };
        };

        packages = with pkgs; [
          gcc gnumake gdb clang-tools bear
        ];

        #env = [
        #  { name = "ENV"; value = "val"; }
        #];

        commands = [
          {
            category = "aoc";
            name = "start";
            help = "Deploy directory structure for day $1";
            command = ''
              BASE="$(printf %02d $1)"
              [ -d "$BASE" ] && echo "Existing directory '$BASE'" && exit 1
              mkdir -p "$BASE" && cd $_
              mkdir -p src resources

              cat << EOF >> src/part1.cpp
              #include <iostream>
              #include <fstream>

              int main(void)
              {
                  int answer{};

                  std::ifstream input{ "resources/input_small.txt" };
                  if (input.is_open())
                  {
                      std::string line;
                      while (not std::getline(input,line).eof())
                      {
                          /* ... */
                      }
                  }
                  input.close();

                  /* ... */

                  std::cout << answer << std::endl;
                  return 0;
              }
              EOF
              cp src/part1.cpp src/part2.cpp

              cat << EOF >> Makefile
              CXXFLAGS := -std=c++17
              EXE := part1 part2

              .PHONY: all clean configure

              all: \$(EXE)

              configure:
              	bear -- \$(MAKE) all

              %.o: %.cpp
              	\$(CXX) -c \$(CXXFLAGS) $< -o $@

              clean:
              	rm -rf \$(EXE) src/*.o compile_commands.json

              %: src/%.o
              	\$(CXX) $^ -o $@
              EOF
            '';
          }
        ];
      };
  });
}