Skip to content

Commit

Permalink
cli
Browse files Browse the repository at this point in the history
  • Loading branch information
domenkozar committed Aug 10, 2023
1 parent bb2bad5 commit 3806e2b
Show file tree
Hide file tree
Showing 43 changed files with 766 additions and 448 deletions.
2 changes: 1 addition & 1 deletion .envrc
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ set -euo pipefail
# External users should use `source_url` to load this file
source_env ./direnvrc

use devenv
use devenv
8 changes: 4 additions & 4 deletions .github/workflows/buildtest.yml
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,12 @@ jobs:
authToken: '${{ secrets.CACHIX_AUTH_TOKEN }}'
- run: |
nix profile remove '.*'
nix profile install --accept-flake-config . "nixpkgs#gawk"
nix profile install --accept-flake-config .
- name: Disable package aliases
run: |
mkdir -p ~/.config/nixpkgs
echo '{ allowAliases = false; }' > ~/.config/nixpkgs/config.nix
- run: devenv shell devenv-test-example ${{ matrix.example }}
- run: devenv test ${{ matrix.example }}
direnv:
name: direnv (${{ join(matrix.os) }})
needs: build
Expand All @@ -92,15 +92,15 @@ jobs:
authToken: '${{ secrets.CACHIX_AUTH_TOKEN }}'
- run: |
mv ./examples/simple/devenv.yaml ./examples/simple/devenv.yaml.orig
awk '
nix run nixpkgs#gawk -- '
{ print }
/^inputs:$/ {
print " devenv:";
print " url: path:../../src/modules";
}
' ./examples/simple/devenv.yaml.orig > ./examples/simple/devenv.yaml
nix profile remove '.*'
nix profile install . 'nixpkgs#direnv' nix profile install --accept-flake-config
nix profile install . 'nixpkgs#direnv'
mkdir -p ~/.config/direnv/
cat > ~/.config/direnv/direnv.toml << 'EOF'
[global]
Expand Down
12 changes: 6 additions & 6 deletions devenv.lock
Original file line number Diff line number Diff line change
Expand Up @@ -107,11 +107,11 @@
},
"nixpkgs": {
"locked": {
"lastModified": 1689261696,
"narHash": "sha256-LzfUtFs9MQRvIoQ3MfgSuipBVMXslMPH/vZ+nM40LkA=",
"lastModified": 1690952720,
"narHash": "sha256-fPsiQHARfhVxXpWgcuSKvkYwSco8K13K7XevBpdIfPg=",
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "df1eee2aa65052a18121ed4971081576b25d6b5c",
"rev": "96112a3ed5d12bb1758b42c63b924c004b6c0bc9",
"type": "github"
},
"original": {
Expand Down Expand Up @@ -164,11 +164,11 @@
"nixpkgs-stable": "nixpkgs-stable"
},
"locked": {
"lastModified": 1688596063,
"narHash": "sha256-9t7RxBiKWHygsqXtiNATTJt4lim/oSYZV3RG8OjDDng=",
"lastModified": 1690743255,
"narHash": "sha256-dsJzQsyJGWCym1+LMyj2rbYmvjYmzeOrk7ypPrSFOPo=",
"owner": "cachix",
"repo": "pre-commit-hooks.nix",
"rev": "c8d18ba345730019c3faf412c96a045ade171895",
"rev": "fcbf4705d98398d084e6cb1c826a0b90a91d22d7",
"type": "github"
},
"original": {
Expand Down
51 changes: 11 additions & 40 deletions devenv.nix
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
{ inputs, pkgs, lib, config, ... }:

{
env = {
DEVENV_NIX = inputs.nix.packages.${pkgs.stdenv.system}.nix;
};
env.DEVENV_NIX = inputs.nix.packages.${pkgs.stdenv.system}.nix;
# TODO: manylinux for python
env.LD_LIBRARY_PATH = config.devenv.dotfile + "/profile/lib";

packages = [
pkgs.cairo
pkgs.xorg.libxcb
pkgs.yaml2json
pkgs.tesh
];

languages.nix.enable = true;
Expand Down Expand Up @@ -44,7 +45,11 @@
tmp="$(mktemp -d)"
devenv init "$tmp"
pushd "$tmp"
echo -e " devenv:\n url: path:${config.devenv.root}/src/modules" >> devenv.yaml
cat devenv.yaml
devenv version
devenv ci
devenv test
popd
rm -rf "$tmp"
Expand Down Expand Up @@ -82,43 +87,6 @@
popd
rm -rf "$tmp"
'';
scripts.devenv-test-all-examples.exec = ''
for dir in $(ls examples); do
devenv-test-example $dir
done
'';
scripts.devenv-test-example.exec = ''
# execute all trap_ function on exit
trap 'eval $(declare -F | grep -o "trap_[^ ]*" | tr "\n" ";")' EXIT
set -e
example="$PWD/examples/$1"
pushd $example
mv devenv.yaml devenv.yaml.orig
awk '
{ print }
/^inputs:$/ {
print " devenv:";
print " url: path:../../src/modules";
}
' devenv.yaml.orig > devenv.yaml
trap_restore_yaml() {
mv "$example/devenv.yaml.orig" "$example/devenv.yaml"
}
devenv ci
if [ -f .test.sh ]; then
trap_restore_local() {
rm "$example/devenv.local.nix"
rm -rf "$example/.devenv"
}
# coreutils-full provides timeout on darwin
echo "{ pkgs, ... }: { packages = [ pkgs.coreutils-full ]; }" > devenv.local.nix
devenv shell ./.test.sh
else
devenv shell ls
fi
popd
'';
scripts."devenv-generate-doc-options".exec = ''
set -e
options=$(nix build --impure --extra-experimental-features 'flakes nix-command' --show-trace --print-out-paths --no-link '.#devenv-docs-options')
Expand Down Expand Up @@ -162,4 +130,7 @@
MD033 = false;
MD034 = false;
};

tests = config.lib.mkTests ./examples
// config.lib.mkTests ./tests;
}
4 changes: 2 additions & 2 deletions devenv.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ inputs:
url: github:domenkozar/nix/relaxed-flakes
inputs:
nixpkgs:
follows: "nixpkgs"
follows: nixpkgs
devenv:
url: path:./src/modules
pre-commit-hooks:
url: github:cachix/pre-commit-hooks.nix
inputs:
nixpkgs:
follows: "nixpkgs"
follows: nixpkgs
34 changes: 34 additions & 0 deletions docs/binary-caching.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
Typically [packages](./packages.md) come prebuilt with binaries provided by [the official binary cache](https://cache.nixos.org).

If you're modifying a package or using a package that's not built upstream,
Nix will build it from source instead of downloading a binary.

To prevent packages from being built more than once, there's seamless integration with
binary caches using [Cachix](https://cachix.org).

## Setup

If you'd like to push binaries to your own cache, you'll need [to create one](https://app.cachix.org/cache).

After that you'll need to set `cachix authtoken XXX` with either [a personal auth token](https://app.cachix.org/personal-auth-tokens) or a cache token (that you can create in cache settings).

## devenv.nix

To specify `pre-commit-hooks` as a cache to pull from and `mycache` to pull from and push to:

```nix title="devenv.nix"
{
cachix.pull = [ "pre-commit-hooks" ];
cachix.push = "mycache";
}
```

# Pushing only in specific cases

You'll likely not want every user to push to the cache.

It's usually convenient to push [explicitly](./files-and-variables/#devenvlocalnix), for example as part of CI run:

```shell-session
$ echo '{ cachix.push = "mycache"; }' > devenv.local.nix
```
17 changes: 6 additions & 11 deletions docs/community/contributing.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,23 @@ We have a rule that new features need to come with documentation and tests (`dev

## Preparing the `devenv` development environment

1. Follow the [installation instructions for Nix and Cachix](../../getting-started/#installation).
1. Follow the [installation instructions for Nix and Cachix](../../getting-started/#installation) and [install direnv](../../automatic-shell-activation/).

2. `git clone https://github.com/cachix/devenv.git`

3. `cd devenv`

4. To build the project, run `nix-build`.

5. `./result/bin/devenv shell`

6. Once you have made changes, run `./result/bin/devenv shell` again.

To automate this workflow, [install and use direnv](../../automatic-shell-activation/).
4. To build the project, run `direnv allow .`.

## Repository structure

- The `devenv` CLI is in `src/devenv.nix`.
- The `flake.nix` auto-generation logic lies in `src/flake.nix`.
- The `devenv` CLI is in `src/devenv/cli.py`.
- The `flake.nix` auto-generation logic lies in `src/modules/flake.tmpl.nix`.
- All modules related to `devenv.nix` are in `src/modules/`.
- Examples are automatically tested on CI and are the best way to work on developing new modules, see `examples/`.
- Examples are automatically tested on CI and are the best way to work on developing new modules, see `examples/` and `tests/`
- Documentation is in `docs/`.
- To run a development server, run `devenv up`.
- To run a test, run `devnenv test <example-name>`.

## Contributing language improvements

Expand Down
55 changes: 55 additions & 0 deletions docs/tests.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
To ease testing of your environments,
we provide a way to define the tests and to run them.

## Writing devenv tests

A simple test would look like:

```nix title="devenv.nix"
{ pkgs, ... }: {
tests.basic = {
nix = ''
{ pkgs, ... }: {
packages = [ pkgs.ncdu ];
}
'';
test = ''
ncdu --version | grep "ncdu 2.2"
'';
};
}
```

```shell-session
$ devenv test
✔ Gathering tests in 0.3s.
• Found 1 test(s), running 1:
• Testing basic ...
• Running $ devenv ci
• Running .test.sh.
✔ Running basic in 16.7s.
```

## Defining tests in a folder

A simple test with a test script:

```shell-session
$ ls tests/mytest/
.test.sh devenv.nix devenv.yaml
```

Define tests:

```nix title="devenv.nix"
{ config, ... }: {
tests = config.lib.mkTests ./tests;
}
```

Run tests:

```shell-session
$ devenv test
...
```
7 changes: 2 additions & 5 deletions examples/clickhouse/.test.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
#!/bin/sh
set -ex
pkill clickhouse
devenv up&
set -xe
timeout 20 bash -c 'until echo > /dev/tcp/localhost/9000; do sleep 0.5; done'
clickhouse-client --query "SELECT 1"
clickhouse-client --query "SELECT 1"
2 changes: 1 addition & 1 deletion examples/clickhouse/devenv.nix
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
services.clickhouse = {
enable = true;
config = ''
http_port: 9000
# http_port: 8123
'';
};
}
4 changes: 0 additions & 4 deletions examples/mailpit/.test.sh
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
#!/bin/sh
set -ex

devenv up &
DEVENV_PID=$!
trap "pkill -P $DEVENV_PID" EXIT

timeout 20 bash -c 'until echo > /dev/tcp/localhost/1025; do sleep 0.5; done'

sendmail john@example.com <<EOF
Expand Down
7 changes: 1 addition & 6 deletions examples/minio/.test.sh
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
#!/bin/sh
set -ex

devenv up &
DEVENV_PID=$!
trap "pkill -P $DEVENV_PID" EXIT

timeout 20 bash -c 'until echo > /dev/tcp/localhost/9000; do sleep 0.5; done'

mc admin info local
mc admin info local
7 changes: 1 addition & 6 deletions examples/mysql/.test.sh
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
#!/bin/sh
set -ex

devenv up &
DEVENV_PID=$!
trap "pkill -P $DEVENV_PID" EXIT

timeout 60 bash -c 'until MYSQL_PWD="" mysql -u root test_database < /dev/null; do sleep 0.5; done'
timeout 60 bash -c 'until MYSQL_PWD="" mysql -u root test_database < /dev/null; do sleep 0.5; done'
8 changes: 6 additions & 2 deletions examples/overlays/devenv.nix
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
{ pkgs, ... }:

{
packages = [ pkgs.rust-bin.stable.latest.default ];
packages = [
# from the rust-overlay
pkgs.rust-bin.stable.latest.default

services.blackfire.enable = true;
# from subflake
pkgs.hello2
];
}
4 changes: 4 additions & 0 deletions examples/overlays/devenv.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ allowUnfree: true
inputs:
nixpkgs:
url: github:NixOS/nixpkgs/nixpkgs-unstable
subflake:
url: path:./subflake
overlays:
- default
rust-overlay:
url: github:oxalica/rust-overlay
overlays:
Expand Down
7 changes: 7 additions & 0 deletions examples/overlays/subflake/flake.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
outputs = { ... }: {
overlays.default = self: super: {
hello2 = self.hello;
};
};
}
4 changes: 0 additions & 4 deletions examples/postgres/.test.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,2 @@
#!/bin/sh
set -ex

devenv up &

timeout 20 bash -c 'until psql -h /tmp -c "SELECT 1" mydb 2>/dev/null; do sleep 0.5; done'
Loading

0 comments on commit 3806e2b

Please sign in to comment.