├── .github └── workflows │ └── ci.yml ├── .gitignore ├── LICENSE ├── README.md ├── nix-docker.yml ├── nix.yml └── tests ├── .envrc ├── flake.lock └── flake.nix /.github/workflows/ci.yml: -------------------------------------------------------------------------------- 1 | on: 2 | push: 3 | branches: 4 | - master 5 | paths-ignore: 6 | - '**.md' 7 | pull_request: 8 | paths-ignore: 9 | - '**.md' 10 | workflow_dispatch: 11 | 12 | name: CI 13 | 14 | jobs: 15 | test: 16 | name: Test 17 | runs-on: macos-latest 18 | steps: 19 | - name: Checkout sources 20 | uses: actions/checkout@v2 21 | - name: Install lima 22 | run: brew install lima 23 | - name: Create VM from template 24 | run: limactl start --name=default nix.yml 25 | - name: Test VM 26 | run: cd tests && lima direnv exec . hello -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .direnv 2 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2022 Joshua Gilman 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # lima-nix 2 | 3 |

4 | 5 | 6 | 7 | 8 | 9 | 10 |

11 | 12 | This repository contains a [Lima] template that will create a Ubuntu 13 | VM with [nix] and [direnv] automatically installed and configured. 14 | 15 | ## Usage 16 | 17 | To use the template: 18 | 19 | ```text 20 | limactl start --name=default https://raw.githubusercontent.com/jmgilman/lima-nix/v0.2.0/nix.yml 21 | ``` 22 | 23 | This template is optimized for use with [nix-direnv]. In a folder with a 24 | properly configured `.envrc` you should be able to run: 25 | 26 | ```text 27 | lima 28 | ``` 29 | 30 | And you will (after a short time) be dropped into the appropriate development 31 | shell. Alternatively, you can directly run commands with the `direnv` context 32 | activated using the following: 33 | 34 | ```text 35 | lima direnv exec . 36 | ``` 37 | 38 | ### Docker 39 | 40 | A variant of the template is provided that installs and forwards Docker. To use 41 | it: 42 | 43 | ```text 44 | limactl start --name=default https://raw.githubusercontent.com/jmgilman/lima-nix/v0.2.0/nix-docker.yml 45 | ``` 46 | 47 | ## Contributing 48 | 49 | Check out the [issues] for items needing attention or submit your own and 50 | then: 51 | 52 | 1. Fork the repo () 53 | 2. Create your feature branch (git checkout -b feature/fooBar) 54 | 3. Commit your changes (git commit -am 'Add some fooBar') 55 | 4. Push to the branch (git push origin feature/fooBar) 56 | 5. Create a new Pull Request 57 | 58 | [direnv]: https://direnv.net 59 | [issues]: https://github.com/jmgilman/lima-nix/issues 60 | [lima]: https://github.com/lima-vm/lima 61 | [nix]: https://nixos.org 62 | [nix-direnv]: https://github.com/nix-community/nix-direnv 63 | -------------------------------------------------------------------------------- /nix-docker.yml: -------------------------------------------------------------------------------- 1 | images: 2 | - location: "https://cloud-images.ubuntu.com/releases/22.04/release-20220902/ubuntu-22.04-server-cloudimg-amd64.img" 3 | arch: "x86_64" 4 | digest: "sha256:c777670007cc5f132417b9e0bc01367ccfc2a989951ffa225bb1952917c3aa81" 5 | - location: "https://cloud-images.ubuntu.com/releases/22.04/release-20220902/ubuntu-22.04-server-cloudimg-arm64.img" 6 | arch: "aarch64" 7 | digest: "sha256:9620f479bd5a6cbf1e805654d41b27f4fc56ef20f916c8331558241734de81ae" 8 | - location: "https://cloud-images.ubuntu.com/releases/22.04/release/ubuntu-22.04-server-cloudimg-amd64.img" 9 | arch: "x86_64" 10 | - location: "https://cloud-images.ubuntu.com/releases/22.04/release/ubuntu-22.04-server-cloudimg-arm64.img" 11 | arch: "aarch64" 12 | 13 | mounts: 14 | - location: "~" 15 | writable: true 16 | - location: "/tmp/lima" 17 | writable: true 18 | 19 | containerd: 20 | system: false 21 | user: false 22 | 23 | provision: 24 | - mode: system 25 | script: | 26 | #!/bin/bash 27 | 28 | set -euo pipefail 29 | 30 | DOCKER_CONFIG=$( 31 | cat <&/dev/null; then 39 | apt-get update && apt-get install -y direnv 40 | fi 41 | 42 | # Install Docker 43 | if ! command -v docker &>/dev/null; then 44 | mkdir -p /etc/systemd/system/docker.socket.d 45 | echo "${DOCKER_CONFIG}" >/etc/systemd/system/docker.socket.d/override.conf 46 | 47 | export DEBIAN_FRONTEND=noninteractive 48 | curl -fsSL https://get.docker.com | sh 49 | fi 50 | 51 | - mode: user 52 | script: | 53 | #!/bin/bash 54 | 55 | set -euo pipefail 56 | 57 | NIX_INSTALLER=https://nixos.org/nix/install 58 | NIX_CONFIG=$( 59 | cat <<'EOF' 60 | sandbox = false 61 | experimental-features = nix-command flakes 62 | EOF 63 | ) 64 | DIRENV_CONFIG=$( 65 | cat <<'EOF' 66 | [global] 67 | warn_timeout = "10m" 68 | [whitelist] 69 | prefix = [ "/" ] 70 | EOF 71 | ) 72 | NIX_DIRENV=$( 73 | cat <<'EOF' 74 | if ! has nix_direnv_version || ! nix_direnv_version 2.1.1; then 75 | source_url "https://raw.githubusercontent.com/nix-community/nix-direnv/2.1.1/direnvrc" "sha256-b6qJ4r34rbE23yWjMqbmu3ia2z4b2wIlZUksBke/ol0=" 76 | fi 77 | EOF 78 | ) 79 | 80 | # Install Nix 81 | if ! command -v nix &>/dev/null; then 82 | sh <(curl -L "${NIX_INSTALLER}") --daemon 83 | fi 84 | 85 | # Configure Nix 86 | if [[ ! -d "${HOME}/.config/nix" ]]; then 87 | mkdir -p "${HOME}/.config/nix" 88 | echo "${NIX_CONFIG}" >"${HOME}/.config/nix/nix.conf" 89 | fi 90 | 91 | # Configure direnv 92 | if [[ ! -d "$HOME/.config/direnv" ]]; then 93 | mkdir -p "${HOME}/.config/direnv" 94 | echo "${DIRENV_CONFIG}" >"${HOME}/.config/direnv/config.toml" 95 | echo "${NIX_DIRENV}" >"${HOME}/.config/direnv/direnvrc" 96 | echo 'eval "$(direnv hook bash)"' >>"${HOME}/.bashrc" 97 | fi 98 | 99 | hostResolver: 100 | hosts: 101 | host.docker.internal: host.lima.internal 102 | portForwards: 103 | - guestSocket: "/var/run/docker.sock" 104 | hostSocket: "{{.Dir}}/sock/docker.sock" 105 | message: | 106 | To run `docker` on the host (assumes docker-cli is installed), run the following commands: 107 | ------ 108 | docker context create lima-{{.Name}} --docker "host=unix://{{.Dir}}/sock/docker.sock" 109 | docker context use lima-{{.Name}} 110 | docker run hello-world 111 | ------ -------------------------------------------------------------------------------- /nix.yml: -------------------------------------------------------------------------------- 1 | images: 2 | - location: "https://cloud-images.ubuntu.com/releases/22.04/release-20220902/ubuntu-22.04-server-cloudimg-amd64.img" 3 | arch: "x86_64" 4 | digest: "sha256:c777670007cc5f132417b9e0bc01367ccfc2a989951ffa225bb1952917c3aa81" 5 | - location: "https://cloud-images.ubuntu.com/releases/22.04/release-20220902/ubuntu-22.04-server-cloudimg-arm64.img" 6 | arch: "aarch64" 7 | digest: "sha256:9620f479bd5a6cbf1e805654d41b27f4fc56ef20f916c8331558241734de81ae" 8 | - location: "https://cloud-images.ubuntu.com/releases/22.04/release/ubuntu-22.04-server-cloudimg-amd64.img" 9 | arch: "x86_64" 10 | - location: "https://cloud-images.ubuntu.com/releases/22.04/release/ubuntu-22.04-server-cloudimg-arm64.img" 11 | arch: "aarch64" 12 | 13 | mounts: 14 | - location: "~" 15 | writable: true 16 | - location: "/tmp/lima" 17 | writable: true 18 | 19 | # Don't include containerd by default 20 | containerd: 21 | system: false 22 | user: false 23 | 24 | provision: 25 | - mode: system 26 | script: | 27 | #!/bin/bash 28 | 29 | set -euo pipefail 30 | 31 | # Install direnv 32 | if ! dpkg -s direnv >&/dev/null; then 33 | apt-get update && apt-get install -y direnv 34 | fi 35 | 36 | - mode: user 37 | script: | 38 | #!/bin/bash 39 | 40 | set -euo pipefail 41 | 42 | NIX_INSTALLER=https://nixos.org/nix/install 43 | NIX_CONFIG=$( 44 | cat <<'EOF' 45 | sandbox = false 46 | experimental-features = nix-command flakes 47 | EOF 48 | ) 49 | DIRENV_CONFIG=$( 50 | cat <<'EOF' 51 | [global] 52 | warn_timeout = "10m" 53 | [whitelist] 54 | prefix = [ "/" ] 55 | EOF 56 | ) 57 | NIX_DIRENV=$( 58 | cat <<'EOF' 59 | if ! has nix_direnv_version || ! nix_direnv_version 2.1.1; then 60 | source_url "https://raw.githubusercontent.com/nix-community/nix-direnv/2.1.1/direnvrc" "sha256-b6qJ4r34rbE23yWjMqbmu3ia2z4b2wIlZUksBke/ol0=" 61 | fi 62 | EOF 63 | ) 64 | 65 | # Install Nix 66 | if ! command -v nix &>/dev/null; then 67 | sh <(curl -L "${NIX_INSTALLER}") --daemon 68 | fi 69 | 70 | # Configure Nix 71 | if [[ ! -d "${HOME}/.config/nix" ]]; then 72 | mkdir -p "${HOME}/.config/nix" 73 | echo "${NIX_CONFIG}" >"${HOME}/.config/nix/nix.conf" 74 | fi 75 | 76 | # Configure direnv 77 | if [[ ! -d "$HOME/.config/direnv" ]]; then 78 | mkdir -p "${HOME}/.config/direnv" 79 | echo "${DIRENV_CONFIG}" >"${HOME}/.config/direnv/config.toml" 80 | echo "${NIX_DIRENV}" >"${HOME}/.config/direnv/direnvrc" 81 | echo 'eval "$(direnv hook bash)"' >>"${HOME}/.bashrc" 82 | fi 83 | -------------------------------------------------------------------------------- /tests/.envrc: -------------------------------------------------------------------------------- 1 | use flake 2 | -------------------------------------------------------------------------------- /tests/flake.lock: -------------------------------------------------------------------------------- 1 | { 2 | "nodes": { 3 | "flake-utils": { 4 | "locked": { 5 | "lastModified": 1659877975, 6 | "narHash": "sha256-zllb8aq3YO3h8B/U0/J1WBgAL8EX5yWf5pMj3G0NAmc=", 7 | "owner": "numtide", 8 | "repo": "flake-utils", 9 | "rev": "c0e246b9b83f637f4681389ecabcb2681b4f3af0", 10 | "type": "github" 11 | }, 12 | "original": { 13 | "owner": "numtide", 14 | "repo": "flake-utils", 15 | "type": "github" 16 | } 17 | }, 18 | "nixpkgs": { 19 | "locked": { 20 | "lastModified": 1664847737, 21 | "narHash": "sha256-Wxl0CtRH3Vo8+qEZ/PbCcx+9D8wEEi56tJPmROum2ss=", 22 | "owner": "NixOS", 23 | "repo": "nixpkgs", 24 | "rev": "de80d1d04ee691279e1302a1128c082bbda3ab01", 25 | "type": "github" 26 | }, 27 | "original": { 28 | "id": "nixpkgs", 29 | "type": "indirect" 30 | } 31 | }, 32 | "root": { 33 | "inputs": { 34 | "flake-utils": "flake-utils", 35 | "nixpkgs": "nixpkgs" 36 | } 37 | } 38 | }, 39 | "root": "root", 40 | "version": 7 41 | } 42 | -------------------------------------------------------------------------------- /tests/flake.nix: -------------------------------------------------------------------------------- 1 | { 2 | description = "my project description"; 3 | 4 | inputs.flake-utils.url = "github:numtide/flake-utils"; 5 | 6 | outputs = { self, nixpkgs, flake-utils }: 7 | flake-utils.lib.eachDefaultSystem 8 | (system: 9 | let pkgs = nixpkgs.legacyPackages.${system}; in 10 | { 11 | devShells.default = pkgs.mkShell { 12 | buildInputs = with pkgs; [ hello ]; 13 | }; 14 | } 15 | ); 16 | } 17 | --------------------------------------------------------------------------------