Skip to content

Instantly share code, notes, and snippets.

@DianQK
Created May 23, 2024 14:33
Show Gist options
  • Save DianQK/b1bcf81f4bdc7129c47874e090062e58 to your computer and use it in GitHub Desktop.
Save DianQK/b1bcf81f4bdc7129c47874e090062e58 to your computer and use it in GitHub Desktop.
# Shell for bootstrapping flake-enabled nix and home-manager
# You can enter it through 'nix develop' or (legacy) 'nix-shell'
{
nixpkgs ? <nixpkgs>,
system ? builtins.currentSystem,
}: let
pkgs = import nixpkgs {
inherit system;
config.android_sdk.accept_license = true;
config.allowUnfree = true;
# crossSystem = {
# config = "mipsel-unknown-linux-gnu";
# };
};
in {
default = pkgs.mkShell {
name = "my-nix";
# Enable experimental features without having to specify the argument
NIX_CONFIG = "experimental-features = nix-command flakes";
nativeBuildInputs = with pkgs; [nix home-manager git];
};
# cmake $=cmakeFlags ../llvm-project/llvm
llvm-dev = let
stdenv = pkgs.stdenv;
gccForLibs = stdenv.cc.cc;
targetPlatform = stdenv.targetPlatform;
clang-mkShell =
pkgs.mkShell.override {stdenv = pkgs.llvmPackages_18.stdenv;};
libPath = with pkgs;
lib.makeLibraryPath [
gccForLibs.lib # libstdc++.so.6
zlib # libz.so.1
ncurses # libncursesw.so.6
libintl
];
in
clang-mkShell {
name = "llvm-dev";
nativeBuildInputs = with pkgs;
[
python3
ninja
cmake
graphviz
sccache
ccache
# llvm-test-suite
libintl
tcl
valgrind
clang-tools_18 # for clangd to find the correct headers
z3
re2c
creduce
]
++ (with llvmPackages_18; [llvm lld bintools]);
buildInputs = with pkgs; [stdenv.cc.cc.lib libintl];
shellHook = ''
export LLVM_PROJECT=$PWD/llvm-project
'';
# where to find libgcc
NIX_LDFLAGS = "-L${gccForLibs}/lib/gcc/${targetPlatform.config}/${gccForLibs.version}";
LD_LIBRARY_PATH = "${libPath}";
SCCACHE_CACHE_SIZE = "200G";
GCC_LIB_FLAGS = "-B${gccForLibs}/lib/gcc/${targetPlatform.config}/${gccForLibs.version}";
# teach clang about C startup file locations
CFLAGS = "-B${gccForLibs}/lib/gcc/${targetPlatform.config}/${gccForLibs.version} -B${stdenv.cc.libc}/lib";
# llvm-compile-time
LLVM_BINUTILS_INCDIR = "${gccForLibs}/lib/gcc/${targetPlatform.config}/${gccForLibs.version}/plugin/include";
# cmake $=cmakeFlags -S llvm -B build -G Ninja
cmakeFlags = [
"-DCMAKE_C_COMPILER_LAUNCHER=sccache"
"-DCMAKE_CXX_COMPILER_LAUNCHER=sccache"
"-DCMAKE_BUILD_TYPE=RelWithDebInfo"
"-DLLVM_ENABLE_ASSERTIONS=ON"
"-DLLVM_USE_LINKER=lld"
"-DLLVM_TARGETS_TO_BUILD=host"
"-DBUILD_SHARED_LIBS=ON"
"-DLLVM_ENABLE_PROJECTS=clang"
"-GNinja"
];
cmakeAlive2Flags = [
"-DLLVM_ENABLE_RTTI=ON"
"-DLLVM_ENABLE_EH=ON"
];
cmakeFullFlags = [
"-DCMAKE_C_COMPILER_LAUNCHER=sccache"
"-DCMAKE_CXX_COMPILER_LAUNCHER=sccache"
# Debug for debug builds
"-DCMAKE_BUILD_TYPE=RelWithDebInfo"
"-DLLVM_ENABLE_ASSERTIONS=ON"
"-DLLVM_USE_LINKER=lld"
"-DBUILD_SHARED_LIBS=ON"
# inst will be our installation prefix
# "-DCMAKE_INSTALL_PREFIX=../inst"
# "-DLLVM_INSTALL_TOOLCHAIN_ONLY=ON"
# change this to enable the projects you need
# enable libcxx* to come into play at runtimes
# "-DLLVM_ENABLE_RUNTIMES=libcxx;libcxxabi"
# this makes llvm only to produce code for the current platform, this saves CPU time, change it to what you need
# "-DLLVM_TARGETS_TO_BUILD=host"
# "-DLLVM_TARGETS_TO_BUILD=AArch64;ARM;RISCV;X86"
# "-DLLVM_APPEND_VC_REV=false"
# "-DLLVM_ENABLE_PROJECTS=clang"
# "-DCLANG_ENABLE_ARCMT=false"
# "-DCLANG_ENABLE_STATIC_ANALYZER=false"
"-GNinja"
];
# cmake $=cmakeFlags $=cmakeSanFlags -S llvm -B build -G Ninja
cmakeSanFlags = [
"-DLLVM_USE_SANITIZER=Address;Undefined"
];
};
rustc-dev = let
stdenv = pkgs.stdenv;
gccForLibs = stdenv.cc.cc;
gccForlibc = stdenv.cc.libc;
targetPlatform = stdenv.targetPlatform;
libPath = with pkgs;
lib.makeLibraryPath [
gccForLibs.lib # libstdc++.so.6
gcc
libgcc
glibc
gccForlibc
zlib # libz.so.1
ncurses # libncursesw.so.6
libintl
openssl
curl
];
# libclang/lldb broken from llvm 15/16, python import also broken.
# libPath = with pkgs;
# lib.makeLibraryPath [
# llvmPackages_18.libclang
# ];
clang-mkShell =
pkgs.mkShell.override {stdenv = pkgs.llvmPackages_18.stdenv;};
# Build configuration for rust-lang/rust. Based on `config.example.toml` (then called
# `config.toml.example`) from `1bd30ce2aac40c7698aa4a1b9520aa649ff2d1c5`
config = pkgs.writeText "rustc-config" ''
profile = "compiler"
change-id = 121278
[build]
patch-binaries-for-nix = true
# The path to (or name of) the GDB executable to use. This is only used for
# executing the debuginfo test suite.
gdb = "gdb"
python = "python"
submodules = false
build = "x86_64-unknown-linux-gnu"
# target = ["x86_64-unknown-linux-gnu", "thumbv6m-none-eabi"]
# cargo = "/home/dianqk/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/bin/cargo"
# profiler = true
[llvm]
ccache = "sccache"
[rust]
incremental = false
debug-logging = true
deny-warnings = false
llvm-tools = true
# debug-assertions = true
'';
ripgrepConfig = let
# Files that are ignored by ripgrep when searching.
ignoreFile = pkgs.writeText "rustc-rgignore" ''
configure
config.example.toml
x.py
LICENSE-MIT
LICENSE-APACHE
COPYRIGHT
**/*.txt
**/*.toml
**/*.yml
**/*.nix
*.md
src/ci
src/etc/
src/llvm-emscripten/
src/llvm-project/
src/rtstartup/
src/rustllvm/
src/stdsimd/
src/tools/rls/rls-analysis/test_data/
'';
in
pkgs.writeText "rustc-ripgreprc" "--ignore-file=${ignoreFile}";
in
clang-mkShell {
name = "rustc";
nativeBuildInputs = with pkgs;
[
gdb
binutils
cmake
ninja
openssl
pkg-config
python3
git
curl
cacert
patchelf
nix
psutils
hugo # compiler-team repo
sccache
clang-tools_18 # for clangd to find the correct headers
# Replace llvmPackages with llvmPackages_X, where X is the latest LLVM version (at the time of writing, 16)
rustup
rustup-toolchain-install-master
cargo-bisect-rustc
]
++ (with llvmPackages_18; [llvm lld bintools]);
buildInputs = with pkgs; [stdenv.cc.cc.lib gcc];
# where to find libgcc
NIX_LDFLAGS = "-L${gccForLibs}/lib/gcc/${targetPlatform.config}/${gccForLibs.version}";
LD_LIBRARY_PATH = "${libPath}";
CFLAGS = "-B${gccForLibs}/lib/gcc/${targetPlatform.config}/${gccForLibs.version} -B${gccForlibc}/lib";
SCCACHE_CACHE_SIZE = "200G";
RIPGREP_CONFIG_PATH = ripgrepConfig;
RUST_BOOTSTRAP_CONFIG = config;
# RUSTC_VERSION = pkgs.lib.readFile ./rust-toolchain;
# https://github.com/rust-lang/rust-bindgen#environment-variables
LIBCLANG_PATH =
pkgs.lib.makeLibraryPath [pkgs.llvmPackages_18.libclang.lib];
shellHook = ''
export PATH=$PATH:''${CARGO_HOME:-~/.cargo}/bin/
export PATH=$PATH:''${RUSTUP_HOME:-~/.rustup}/toolchains/nightly-x86_64-unknown-linux-gnu/bin/
export RUSTC_PROJECT=$PWD/rust
'';
# Add precompiled library to rustc search path
RUSTFLAGS = builtins.map (a: "-L ${a}/lib") [
# add libraries here (e.g. pkgs.libvmi)
];
# Add glibc, clang, glib and other headers to bindgen search path
BINDGEN_EXTRA_CLANG_ARGS =
# Includes with normal include path
(builtins.map (a: ''-I"${a}/include"'') [
# add dev libraries here (e.g. pkgs.libvmi.dev)
pkgs.glibc.dev
])
# Includes with special directory paths
++ [
''
-I"${pkgs.llvmPackages_18.libclang.lib}/lib/clang/${pkgs.llvmPackages_18.libclang.version}/include"''
''-I"${pkgs.glib.dev}/include/glib-2.0"''
"-I${pkgs.glib.out}/lib/glib-2.0/include/"
];
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment