diff --git a/BUILDING_FROM_SOURCE.md b/BUILDING_FROM_SOURCE.md index 889497e02d..8e8eb3f04c 100644 --- a/BUILDING_FROM_SOURCE.md +++ b/BUILDING_FROM_SOURCE.md @@ -2,59 +2,56 @@ ## Using Nix -### On NixOS - -NixOS users should make use of the nix flake by [enabling nix flakes](https://nixos.wiki/wiki/Flakes). Shell creation can be done by executing `nix develop` from the root of the repo. NixOS users that do not make use of this flake will get stuck on issue #1846. - -### On Linux/MacOS x86_64/aarch64 +### On Linux/MacOS/NixOS x86_64/aarch64/arm64 #### Install -Using [nix](https://nixos.org/download.html) is a quick way to get an environment bootstrapped with a single command. - -Anyone having trouble installing the proper version of LLVM themselves might also prefer this method. +We highly recommend Using [nix](https://nixos.org/download.html) to automatically install all dependencies necessary to build roc. If you are running ArchLinux or a derivative like Manjaro, you'll need to run `sudo sysctl -w kernel.unprivileged_userns_clone=1` before installing nix. -Install nix: +Install nix (not necessary on NixOS): +``` +curl -L https://nixos.org/nix/install | sh +``` -`curl -L https://nixos.org/nix/install | sh` +Start a fresh terminal session (= open a new terminal). -You will need to start a fresh terminal session to use nix. +install nixFlakes in your environment: +``` +nix-env -iA nixpkgs.nixFlakes +``` + +Edit either `~/.config/nix/nix.conf` or `/etc/nix/nix.conf` and add: +``` +experimental-features = nix-command flakes +``` + + If Nix was installed in multi-user mode, make sure to restart the nix-daemon. + If you don't know how to do this, restarting your computer will also do the job. #### Usage -Now with nix installed, you just need to run one command: - -`nix-shell` - -> This may not output anything for a little while. This is normal, hang in there. Also make sure you are in the roc project root. - -> Also, if you're on NixOS you'll need to enable opengl at the system-wide level. You can do this in configuration.nix with `hardware.opengl.enable = true;`. If you don't do this, nix-shell will fail! - +Now with nix set up, you just need to run one command from the roc project root directory: +``` +nix develop +``` You should be in a shell with everything needed to build already installed. Use `cargo run help` to see all subcommands. To use the `repl` subcommand, execute `cargo run repl`. Use `cargo build` to build the whole project. -> When using `nix-shell`, make sure that if you start `nix-shell` and then run `echo "$PATH" | tr ':' '\n'`, you see the `usr/bin` path listed after all the `/nix/…` paths. Otherwise you might get some nasty rust compilation errors! - #### Extra tips -If you plan on using `nix-shell` regularly, check out [direnv](https://direnv.net/) and [lorri](https://github.com/nix-community/lorri). Whenever you `cd` into `roc/`, they will automatically load the Nix dependencies into your current shell, so you never have to run nix-shell directly! +If you want to load all dependencies automatically whenever you `cd` into `roc`, check out [direnv](https://direnv.net/) and [lorri](https://github.com/nix-community/lorri). +Now you no longer need to execute `nix develop` first. ### Editor The editor is a :construction:WIP:construction: and not ready yet to replace your favorite editor, although if you want to try it out on nix, read on. -`cargo run edit` should work from NixOS, if you use a nix-shell from inside another OS, follow the instructions below. +`cargo run edit` should work from NixOS, if you use another OS, follow the instructions below. -#### from nix flake - -Running the ediotr may fail using the classic nix-shell, we recommend using the nix flake, see [enabling nix flakes](https://nixos.wiki/wiki/Flakes). - -start a nix shell using `nix develop` and follow the instructions below for your graphics configuration. - -##### Nvidia GPU +#### Nvidia GPU ``` nix run --override-input nixpkgs nixpkgs/nixos-21.11 --impure github:guibou/nixGL#nixVulkanNvidia -- cargo run edit @@ -79,32 +76,6 @@ nix run --override-input nixpkgs nixpkgs/nixos-21.11 --impure github:guibou/nixG Check the [nixGL repo](https://github.com/guibou/nixGL) for other graphics configurations. Feel free to ask us for help if you get stuck. -#### using a classic nix-shell - -##### Nvidia GPU - -Outside of a nix shell, execute the following: -``` -nix-channel --add https://github.com/guibou/nixGL/archive/main.tar.gz nixgl && nix-channel --update -nix-env -iA nixgl.auto.nixVulkanNvidia -``` -Running the editor does not work with `nix-shell --pure`, instead run: -``` -nix-shell -``` -460.91.03 may be different for you, type nixVulkanNvidia and press tab to autocomplete for your version. -``` -nixVulkanNvidia-460.91.03 cargo run edit -``` - -##### Integrated Intel Graphics - -nix-shell does not work here, use the flake instead; check the section "Integrated Intel Graphics" under "from nix flake". - -##### Other configs - -Check the [nixGL repo](https://github.com/guibou/nixGL) for other graphics configurations. - ## Troubleshooting Create an issue if you run into problems not listed here. diff --git a/flake.nix b/flake.nix index 1b948ea72d..0388b4111f 100644 --- a/flake.nix +++ b/flake.nix @@ -49,6 +49,29 @@ zig-toolchain = zig.packages.${system}."0.9.1"; + # For debugging LLVM IR + debugir = pkgs.stdenv.mkDerivation { + name = "debugir"; + src = pkgs.fetchFromGitHub { + owner = "vaivaswatha"; + repo = "debugir"; + rev = "b981e0b74872d9896ba447dd6391dfeb63332b80"; + sha256 = "Gzey0SF0NZkpiObk5e29nbc41dn4Olv1dx+6YixaZH0="; + }; + buildInputs = with pkgs; [ cmake libxml2 llvmPackages_13.llvm.dev ]; + buildPhase = '' + mkdir build + cd build + cmake -DLLVM_DIR=${pkgs.llvmPackages_13.llvm.dev} -DCMAKE_BUILD_TYPE=Release ../ + cmake --build ../ + cp ../debugir . + ''; + installPhase = '' + mkdir -p $out/bin + cp debugir $out/bin + ''; + }; + sharedInputs = (with pkgs; [ # build libraries cmake @@ -69,7 +92,7 @@ # faster builds - see https://github.com/rtfeldman/roc/blob/trunk/BUILDING_FROM_SOURCE.md#use-lld-for-the-linker llvmPkgs.lld - # debugir + debugir rust ]); in { diff --git a/nix/debugir.nix b/nix/debugir.nix deleted file mode 100644 index f5251e19ef..0000000000 --- a/nix/debugir.nix +++ /dev/null @@ -1,23 +0,0 @@ -{ pkgs ? import { } }: - -pkgs.stdenv.mkDerivation { - name = "debugir"; - src = pkgs.fetchFromGitHub { - owner = "vaivaswatha"; - repo = "debugir"; - rev = "b981e0b74872d9896ba447dd6391dfeb63332b80"; - sha256 = "Gzey0SF0NZkpiObk5e29nbc41dn4Olv1dx+6YixaZH0="; - }; - buildInputs = with pkgs; [ cmake libxml2 llvmPackages_13.llvm.dev ]; - buildPhase = '' - mkdir build - cd build - cmake -DLLVM_DIR=${pkgs.llvmPackages_13.llvm.dev} -DCMAKE_BUILD_TYPE=Release ../ - cmake --build ../ - cp ../debugir . - ''; - installPhase = '' - mkdir -p $out/bin - cp debugir $out/bin - ''; -} diff --git a/nix/sources.json b/nix/sources.json deleted file mode 100644 index fe360b18d1..0000000000 --- a/nix/sources.json +++ /dev/null @@ -1,38 +0,0 @@ -{ - "niv": { - "branch": "master", - "description": "Easy dependency management for Nix projects.", - "homepage": "https://github.com/nmattia/niv", - "owner": "nmattia", - "repo": "niv", - "rev": "9cb7ef336bb71fd1ca84fc7f2dff15ef4b033f2a", - "sha256": "1ajyqr8zka1zlb25jx1v4xys3zqmdy3prbm1vxlid6ah27a8qnzh", - "type": "tarball", - "url": "https://github.com/nmattia/niv/archive/9cb7ef336bb71fd1ca84fc7f2dff15ef4b033f2a.tar.gz", - "url_template": "https://github.com///archive/.tar.gz" - }, - "nixpkgs": { - "branch": "release-21.11", - "description": "Nix Packages collection", - "homepage": "", - "owner": "NixOS", - "repo": "nixpkgs", - "rev": "ed02c2ba0384b2800db41333045a6fb781f12aac", - "sha256": "040rawxqbpblxpsq73qxlk25my2cm0g3gx1pksiacsj15q5fi84q", - "type": "tarball", - "url": "https://github.com/NixOS/nixpkgs/archive/ed02c2ba0384b2800db41333045a6fb781f12aac.tar.gz", - "url_template": "https://github.com///archive/.tar.gz" - }, - "nixpkgs-unstable": { - "branch": "nixpkgs-unstable", - "description": "Nix Packages collection", - "homepage": "", - "owner": "NixOS", - "repo": "nixpkgs", - "rev": "b283b64580d1872333a99af2b4cef91bb84580cf", - "sha256": "0gmrpfzc622xl1lv3ffaj104j2q3nmia7jywafqmgmrcdm9axkkp", - "type": "tarball", - "url": "https://github.com/NixOS/nixpkgs/archive/b283b64580d1872333a99af2b4cef91bb84580cf.tar.gz", - "url_template": "https://github.com///archive/.tar.gz" - } -} diff --git a/nix/sources.nix b/nix/sources.nix deleted file mode 100644 index 1938409ddd..0000000000 --- a/nix/sources.nix +++ /dev/null @@ -1,174 +0,0 @@ -# This file has been generated by Niv. - -let - - # - # The fetchers. fetch_ fetches specs of type . - # - - fetch_file = pkgs: name: spec: - let - name' = sanitizeName name + "-src"; - in - if spec.builtin or true then - builtins_fetchurl { inherit (spec) url sha256; name = name'; } - else - pkgs.fetchurl { inherit (spec) url sha256; name = name'; }; - - fetch_tarball = pkgs: name: spec: - let - name' = sanitizeName name + "-src"; - in - if spec.builtin or true then - builtins_fetchTarball { name = name'; inherit (spec) url sha256; } - else - pkgs.fetchzip { name = name'; inherit (spec) url sha256; }; - - fetch_git = name: spec: - let - ref = - if spec ? ref then spec.ref else - if spec ? branch then "refs/heads/${spec.branch}" else - if spec ? tag then "refs/tags/${spec.tag}" else - abort "In git source '${name}': Please specify `ref`, `tag` or `branch`!"; - in - builtins.fetchGit { url = spec.repo; inherit (spec) rev; inherit ref; }; - - fetch_local = spec: spec.path; - - fetch_builtin-tarball = name: throw - ''[${name}] The niv type "builtin-tarball" is deprecated. You should instead use `builtin = true`. - $ niv modify ${name} -a type=tarball -a builtin=true''; - - fetch_builtin-url = name: throw - ''[${name}] The niv type "builtin-url" will soon be deprecated. You should instead use `builtin = true`. - $ niv modify ${name} -a type=file -a builtin=true''; - - # - # Various helpers - # - - # https://github.com/NixOS/nixpkgs/pull/83241/files#diff-c6f540a4f3bfa4b0e8b6bafd4cd54e8bR695 - sanitizeName = name: - ( - concatMapStrings (s: if builtins.isList s then "-" else s) - ( - builtins.split "[^[:alnum:]+._?=-]+" - ((x: builtins.elemAt (builtins.match "\\.*(.*)" x) 0) name) - ) - ); - - # The set of packages used when specs are fetched using non-builtins. - mkPkgs = sources: system: - let - sourcesNixpkgs = - import (builtins_fetchTarball { inherit (sources.nixpkgs) url sha256; }) { inherit system; }; - hasNixpkgsPath = builtins.any (x: x.prefix == "nixpkgs") builtins.nixPath; - hasThisAsNixpkgsPath = == ./.; - in - if builtins.hasAttr "nixpkgs" sources - then sourcesNixpkgs - else if hasNixpkgsPath && ! hasThisAsNixpkgsPath then - import {} - else - abort - '' - Please specify either (through -I or NIX_PATH=nixpkgs=...) or - add a package called "nixpkgs" to your sources.json. - ''; - - # The actual fetching function. - fetch = pkgs: name: spec: - - if ! builtins.hasAttr "type" spec then - abort "ERROR: niv spec ${name} does not have a 'type' attribute" - else if spec.type == "file" then fetch_file pkgs name spec - else if spec.type == "tarball" then fetch_tarball pkgs name spec - else if spec.type == "git" then fetch_git name spec - else if spec.type == "local" then fetch_local spec - else if spec.type == "builtin-tarball" then fetch_builtin-tarball name - else if spec.type == "builtin-url" then fetch_builtin-url name - else - abort "ERROR: niv spec ${name} has unknown type ${builtins.toJSON spec.type}"; - - # If the environment variable NIV_OVERRIDE_${name} is set, then use - # the path directly as opposed to the fetched source. - replace = name: drv: - let - saneName = stringAsChars (c: if isNull (builtins.match "[a-zA-Z0-9]" c) then "_" else c) name; - ersatz = builtins.getEnv "NIV_OVERRIDE_${saneName}"; - in - if ersatz == "" then drv else - # this turns the string into an actual Nix path (for both absolute and - # relative paths) - if builtins.substring 0 1 ersatz == "/" then /. + ersatz else /. + builtins.getEnv "PWD" + "/${ersatz}"; - - # Ports of functions for older nix versions - - # a Nix version of mapAttrs if the built-in doesn't exist - mapAttrs = builtins.mapAttrs or ( - f: set: with builtins; - listToAttrs (map (attr: { name = attr; value = f attr set.${attr}; }) (attrNames set)) - ); - - # https://github.com/NixOS/nixpkgs/blob/0258808f5744ca980b9a1f24fe0b1e6f0fecee9c/lib/lists.nix#L295 - range = first: last: if first > last then [] else builtins.genList (n: first + n) (last - first + 1); - - # https://github.com/NixOS/nixpkgs/blob/0258808f5744ca980b9a1f24fe0b1e6f0fecee9c/lib/strings.nix#L257 - stringToCharacters = s: map (p: builtins.substring p 1 s) (range 0 (builtins.stringLength s - 1)); - - # https://github.com/NixOS/nixpkgs/blob/0258808f5744ca980b9a1f24fe0b1e6f0fecee9c/lib/strings.nix#L269 - stringAsChars = f: s: concatStrings (map f (stringToCharacters s)); - concatMapStrings = f: list: concatStrings (map f list); - concatStrings = builtins.concatStringsSep ""; - - # https://github.com/NixOS/nixpkgs/blob/8a9f58a375c401b96da862d969f66429def1d118/lib/attrsets.nix#L331 - optionalAttrs = cond: as: if cond then as else {}; - - # fetchTarball version that is compatible between all the versions of Nix - builtins_fetchTarball = { url, name ? null, sha256 }@attrs: - let - inherit (builtins) lessThan nixVersion fetchTarball; - in - if lessThan nixVersion "1.12" then - fetchTarball ({ inherit url; } // (optionalAttrs (!isNull name) { inherit name; })) - else - fetchTarball attrs; - - # fetchurl version that is compatible between all the versions of Nix - builtins_fetchurl = { url, name ? null, sha256 }@attrs: - let - inherit (builtins) lessThan nixVersion fetchurl; - in - if lessThan nixVersion "1.12" then - fetchurl ({ inherit url; } // (optionalAttrs (!isNull name) { inherit name; })) - else - fetchurl attrs; - - # Create the final "sources" from the config - mkSources = config: - mapAttrs ( - name: spec: - if builtins.hasAttr "outPath" spec - then abort - "The values in sources.json should not have an 'outPath' attribute" - else - spec // { outPath = replace name (fetch config.pkgs name spec); } - ) config.sources; - - # The "config" used by the fetchers - mkConfig = - { sourcesFile ? if builtins.pathExists ./sources.json then ./sources.json else null - , sources ? if isNull sourcesFile then {} else builtins.fromJSON (builtins.readFile sourcesFile) - , system ? builtins.currentSystem - , pkgs ? mkPkgs sources system - }: rec { - # The sources, i.e. the attribute set of spec name to spec - inherit sources; - - # The "pkgs" (evaluated nixpkgs) to use for e.g. non-builtin fetchers - inherit pkgs; - }; - -in -mkSources (mkConfig {}) // { __functor = _: settings: mkSources (mkConfig settings); } diff --git a/nix/zig.nix b/nix/zig.nix deleted file mode 100644 index 5d2ce85315..0000000000 --- a/nix/zig.nix +++ /dev/null @@ -1,41 +0,0 @@ -{ pkgs }: - -let - version = "0.9.1"; - - osName = if pkgs.stdenv.isDarwin then "macos" else "linux"; - - splitSystem = builtins.split "-" builtins.currentSystem; - arch = builtins.elemAt splitSystem 0; - isAarch64 = arch == "aarch64"; - - archiveName = "zig-${osName}-${arch}-${version}"; - - # If your system is not aarch64, we assume it's x86_64 - sha256 = if pkgs.stdenv.isDarwin then - if isAarch64 then - "8c473082b4f0f819f1da05de2dbd0c1e891dff7d85d2c12b6ee876887d438287" # macos-aarch64 - else - "2d94984972d67292b55c1eb1c00de46580e9916575d083003546e9a01166754c" # macos-x86_64 - else if isAarch64 then - "5d99a39cded1870a3fa95d4de4ce68ac2610cca440336cfd252ffdddc2b90e66" # linux-aarch64 - else - "be8da632c1d3273f766b69244d80669fe4f5e27798654681d77c992f17c237d7"; # linux-x86_64 -in pkgs.stdenv.mkDerivation { - pname = "zig"; - version = version; - src = pkgs.fetchurl { - inherit sha256; - name = "${archiveName}.tar.xz"; - url = "https://ziglang.org/download/${version}/${archiveName}.tar.xz"; - }; - phases = [ "unpackPhase" ]; - unpackPhase = '' - mkdir -p $out/bin - tar -xf $src - cp ${archiveName}/zig $out/zig - cp -r ${archiveName}/lib $out/lib - ln -s "$out/zig" "$out/bin/zig" - chmod +x $out/bin/zig - ''; -} diff --git a/shell.nix b/shell.nix index 6b810869c2..ee243e6fa4 100644 --- a/shell.nix +++ b/shell.nix @@ -1,117 +1,5 @@ -{ }: +# We recommend using the flake.nix file, see https://nixos.wiki/wiki/Flakes for instructions. +# This file is kept to maintain compatibility with tools like lorri until they support flakes (https://github.com/target/lorri/issues/460). +{ system ? builtins.currentSystem }: -let - sources = import nix/sources.nix { }; - rust_overlay = import (builtins.fetchTarball "https://github.com/oxalica/rust-overlay/archive/master.tar.gz"); - pkgs = import sources.nixpkgs { - overlays = [ rust_overlay ]; - }; - rust_toolchain = (pkgs.rust-bin.fromRustupToolchainFile ./rust-toolchain.toml); - - #unstable-pkgs = import sources.nixpkgs-unstable { }; - - darwinInputs = with pkgs; - lib.optionals stdenv.isDarwin (with pkgs.darwin.apple_sdk.frameworks; [ - AppKit - CoreFoundation - CoreServices - CoreVideo - Foundation - Metal - Security - ]); - - linuxInputs = with pkgs; - lib.optionals stdenv.isLinux [ - valgrind - vulkan-headers - vulkan-loader - vulkan-tools - vulkan-validation-layers - xorg.libX11 - xorg.libXcursor - xorg.libXrandr - xorg.libXi - xorg.libxcb - alsa-lib - ]; - - llvmPkgs = pkgs.llvmPackages_13; - - zig = import ./nix/zig.nix { inherit pkgs; }; - debugir = import ./nix/debugir.nix { inherit pkgs; }; - - inputs = (with pkgs; [ - # build libraries - cmake - git - python3 - llvmPkgs.llvm.dev - llvmPkgs.clang - libxkbcommon - pkg-config - zig - - # lib deps - libffi - libxml2 - ncurses - zlib - libiconv - - # faster builds - see https://github.com/rtfeldman/roc/blob/trunk/BUILDING_FROM_SOURCE.md#use-lld-for-the-linker - llvmPkgs.lld - debugir - - # meta-tools - # note: niv manages its own nixpkgs so it doesn't need pkgs.callPackage. Do - # `cachix use niv` to get cached builds! - (import sources.niv { }).niv - - # tools for development environment - less - ]) ++ [ - rust_toolchain - ]; - -in pkgs.mkShell { - buildInputs = inputs ++ darwinInputs ++ linuxInputs; - - # Additional Env vars - LLVM_SYS_130_PREFIX = "${llvmPkgs.llvm.dev}"; - NIX_GLIBC_PATH = - if pkgs.stdenv.isLinux then "${pkgs.glibc_multi.out}/lib" else ""; - LD_LIBRARY_PATH = with pkgs; - lib.makeLibraryPath - ([ pkg-config stdenv.cc.cc.lib libffi ncurses zlib ] ++ linuxInputs); - - COREAUDIO_SDK_PATH = if pkgs.stdenv.isDarwin then - # The coreaudio-sys crate is configured to look for things in whatever the - # output of `xcrun --sdk macosx --show-sdk-path` is. However, this does not - # always contain the right frameworks, and it uses system versions instead of - # what we control via Nix. Instead of having to run a lot of extra scripts - # to set our systems up to build, we can just create a SDK directory with - # the same layout as the `MacOSX{version}.sdk` that XCode produces. - pkgs.symlinkJoin { - name = "sdk"; - paths = with pkgs.darwin.apple_sdk.frameworks; [ - AudioToolbox - AudioUnit - CoreAudio - CoreFoundation - CoreMIDI - OpenAL - ]; - postBuild = '' - mkdir $out/System - mv $out/Library $out/System - ''; - } - else - # TODO: I'm not 100% confident that this being blank won't cause issues for - # Nix-on-Linux development. It may be sufficient to use the pkgs.symlinkJoin - # above regardless of system! That'd set us up for cross-compilation as well. - ""; - - -} +(builtins.getFlake (toString ./.)).devShell.${system} \ No newline at end of file