summaryrefslogtreecommitdiff
path: root/nix/flake.nix
blob: cdffdccc69503e3d83af3cad2d2402e19b34ca30 (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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
{
  outputs = { self, nixpkgs }: let
    system = "x86_64-linux";
    pkgs = import nixpkgs { inherit system; config.allowUnfree = true; };

    crossSystem = "arm-linux";
    cross = import nixpkgs {
      inherit system;

      config.allowUnsupportedSystem = true;

      crossSystem = {
        config = "arm-unknown-linux-gnueabi";

        gcc = {
          # > Switch "--with-arch" may not be used with switch "--with-cpu"
          # > make[1]: *** [Makefile:4315: configure-gcc] Error 1
          #arch = "armv4";

          # Ver SA110 en arch/arm/mm/Kconfig, es parecido 
          cpu = "arm810";
        };

        linux-kernel = {
          name = "taller";
          target = "uImage";
          makeFlags = [ "LOADADDR=0x8000" ];
          autoModules = false;
          baseConfig = "taller_defconfig";
        };
      };
    };
  in {
    # Tomado de pkgs/build-support/vm/default.nix
    packages."${crossSystem}".proof-of-concept = cross.makeInitrd {
      contents = [
        {
          symlink = "/init";

          object = with cross; let
            initrdUtils = runCommand "initrd-utils"
              { nativeBuildInputs = [ buildPackages.nukeReferences ];
                allowedReferences = [ "out" ]; # prevent accidents like glibc being included in the initrd
              }
              ''
                mkdir -p $out/bin
                mkdir -p $out/lib

                # Copy what we need from Glibc.
                cp -p ${cross.glibc.out}/lib/ld-linux*.so.? $out/lib
                cp -p ${cross.glibc.out}/lib/libc.so.* $out/lib
                cp -p ${cross.glibc.out}/lib/libm.so.* $out/lib
                cp -p ${cross.glibc.out}/lib/libresolv.so.* $out/lib

                # Copy BusyBox.
                cp -pd ${cross.busybox}/bin/* $out/bin

                # Run patchelf to make the programs refer to the copied libraries.
                for i in $out/bin/* $out/lib/*; do if ! test -L $i; then nuke-refs $i; fi; done

                for i in $out/bin/*; do
                    if [ -f "$i" -a ! -L "$i" ]; then
                        echo "patching $i..."
                        patchelf --set-interpreter $out/lib/ld-linux*.so.? --set-rpath $out/lib $i || true
                    fi
                done
              '';


            path = lib.makeSearchPath "bin" [
              #bashInteractive
              #coreutils-full
              #gnugrep
              #neofetch
              #util-linux
            ];
          in writeScript "init" ''
            #!${initrdUtils}/bin/ash

            export PATH=${initrdUtils}/bin

            mkdir -p /dev /etc /proc /sys
            echo -n > /etc/fstab

            mount -t devtmpfs devtmpfs /dev
            mount -t proc none /proc
            mount -t sysfs none /sys

            exec ash
          '';
        }
      ];
    };

    devShells."${system}" = with pkgs; {
      default = mkShell {
        buildInputs = [
          ncurses
          openssl
          SDL2
        ];

        nativeBuildInputs = [
          binutils
          gcc
          gcc-arm-embedded
          gdb
          gnumake
          gtkwave
          pkg-config
          (python39.withPackages (py: [ py.numpy py.pillow py.matplotlib ]))
          (quartus-prime-lite.override { supportedDevices = [ "Cyclone V" ]; })
          verilator
        ];

        shellHook = ''
          export MAKEFLAGS="AR=gcc-ar"
          export CXXFLAGS="-O3 -flto $(pkg-config --cflags sdl2 ncursesw)"
          export LDFLAGS="-O3 -flto $(pkg-config --libs sdl2 ncursesw)"

          # <https://discourse.nixos.org/t/fonts-in-nix-installed-packages-on-a-non-nixos-system/5871/7>
          export LOCALE_ARCHIVE="${glibcLocales}/lib/locale/locale-archive"
          export FONTCONFIG_FILE="${fontconfig.out}/etc/fonts/fonts.conf"
        '';
      };

      kbuild = mkShell {
        buildInputs = [
          ncurses
        ];

        nativeBuildInputs = [
          bc
          bison
          flex
          gcc
          gcc-arm-embedded
          gnumake
          openssl # Splash de u-boot
          ubootTools
        ];

        shellHook = ''
          export CROSS_COMPILE=arm-none-eabi-
          export MAKEFLAGS="ARCH=arm O=build/taller LOADADDR=0x8000"
        '';
      };
    };
  };
}