From b06c9705a70463721f6fbed8bbec42d1ef3ac398 Mon Sep 17 00:00:00 2001 From: Tobias Happ Date: Sat, 5 Nov 2022 22:41:58 +0100 Subject: [PATCH] add support for default configuration in flake.nix --- CHANGELOG.md | 3 +++ README.md | 7 ++++--- modules/environment/login/login-inner.nix | 2 +- nix-on-droid/nix-on-droid.sh | 10 ++++++++-- templates/advanced/flake.nix | 2 +- templates/home-manager/flake.nix | 2 +- templates/minimal/flake.nix | 2 +- tests/on-device/config-flake-default.nix | 17 +++++++++++++++++ tests/on-device/config-flake.bats | 19 +++++++++++++++---- 9 files changed, 51 insertions(+), 13 deletions(-) create mode 100644 tests/on-device/config-flake-default.nix diff --git a/CHANGELOG.md b/CHANGELOG.md index a1cabd3..82eef02 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -21,6 +21,9 @@ and `config` and `extraModules` are now combined into `modules` * Add option `environment.motd` to edit the startup message that is printed in every shell +* For flake setups, the output `nixOnDroidConfigurations.default` will be used + when `nix-on-droid switch --flake path/to/flake` is called without attribute + name ## Release 22.05 diff --git a/README.md b/README.md index 8e8323e..b72bda0 100644 --- a/README.md +++ b/README.md @@ -192,7 +192,7 @@ A minimal example could look like the following: outputs = { self, nixpkgs, nix-on-droid }: { - nixOnDroidConfigurations.deviceName = nix-on-droid.lib.nixOnDroidConfiguration { + nixOnDroidConfigurations.default = nix-on-droid.lib.nixOnDroidConfiguration { modules = [ ./nix-on-droid.nix ]; }; @@ -208,8 +208,9 @@ nix flake init --template github:t184256/nix-on-droid#advanced ### Usage with `nix-on-droid` -Use `nix-on-droid switch --flake .#device` to build and activate your configuration (`.#device` will expand to -`.#nixOnDroidConfigurations.device`). +Use `nix-on-droid switch --flake path/to/flake#device` to build and activate your configuration (`path/to/flake#device` +will expand to `.#nixOnDroidConfigurations.device`). If you run `nix-on-droid switch --flake path/to/flake`, the +`default` configuration will be used. **Note:** Currently, nix-on-droid can not be built with an pure flake build because of hardcoded store paths for proot. Therefore, every evaluation of a flake configuration will be executed with `--impure` flag. (This behaviour will be diff --git a/modules/environment/login/login-inner.nix b/modules/environment/login/login-inner.nix index 89cf0ab..d663adc 100644 --- a/modules/environment/login/login-inner.nix +++ b/modules/environment/login/login-inner.nix @@ -89,7 +89,7 @@ writeText "login-inner" '' ''} echo "Installing first nix-on-droid generation..." - ${nixCmd} run ${config.build.flake.nix-on-droid} -- switch --flake ${config.user.home}/.config/nix-on-droid#deviceName + ${nixCmd} run ${config.build.flake.nix-on-droid} -- switch --flake ${config.user.home}/.config/nix-on-droid . "${config.user.home}/.nix-profile/etc/profile.d/nix-on-droid-session-init.sh" diff --git a/nix-on-droid/nix-on-droid.sh b/nix-on-droid/nix-on-droid.sh index e63c660..6dab2ee 100644 --- a/nix-on-droid/nix-on-droid.sh +++ b/nix-on-droid/nix-on-droid.sh @@ -58,7 +58,8 @@ function doHelp() { echo " -n|--dry-run Do a dry run, only prints what actions would be taken" echo " -v|--verbose Verbose output" echo " -f|--file FILE Path to config file" - echo " -F|--flake FLAKE Path to flake and device name (e.g. path/to/flake#device)" + echo " -F|--flake FLAKE Path to flake and device name (e.g. path/to/flake#device)," + echo " device 'default' will be used if no attribute name is given" echo echo "Options passed on to nix build" echo @@ -139,7 +140,12 @@ while [[ $# -gt 0 ]]; do PASSTHROUGH_OPTS+=(--extra-experimental-features "flakes nix-command") # add "nixOnDroidConfigurations." as prefix in attribute name, e.g. # /path/to/flake#device -> /path/to/flake#nixOnDroidConfigurations.device - FLAKE_CONFIG_URI="${1%#*}#nixOnDroidConfigurations.${1#*#}" + # if no attribute name given, use "default" + if [[ "$1" =~ \# ]]; then + FLAKE_CONFIG_URI="${1%#*}#nixOnDroidConfigurations.${1#*#}" + else + FLAKE_CONFIG_URI="${1}#nixOnDroidConfigurations.default" + fi shift ;; -h|--help) diff --git a/templates/advanced/flake.nix b/templates/advanced/flake.nix index 7f4eea4..fd81cac 100644 --- a/templates/advanced/flake.nix +++ b/templates/advanced/flake.nix @@ -18,7 +18,7 @@ outputs = { self, nixpkgs, home-manager, nix-on-droid }: { - nixOnDroidConfigurations.deviceName = nix-on-droid.lib.nixOnDroidConfiguration { + nixOnDroidConfigurations.default = nix-on-droid.lib.nixOnDroidConfiguration { modules = [ ./nix-on-droid.nix diff --git a/templates/home-manager/flake.nix b/templates/home-manager/flake.nix index e73f02b..e5e3c55 100644 --- a/templates/home-manager/flake.nix +++ b/templates/home-manager/flake.nix @@ -18,7 +18,7 @@ outputs = { self, nixpkgs, home-manager, nix-on-droid }: { - nixOnDroidConfigurations.deviceName = nix-on-droid.lib.nixOnDroidConfiguration { + nixOnDroidConfigurations.default = nix-on-droid.lib.nixOnDroidConfiguration { modules = [ ./nix-on-droid.nix ]; }; diff --git a/templates/minimal/flake.nix b/templates/minimal/flake.nix index 9b0ed34..3476152 100644 --- a/templates/minimal/flake.nix +++ b/templates/minimal/flake.nix @@ -12,7 +12,7 @@ outputs = { self, nixpkgs, nix-on-droid }: { - nixOnDroidConfigurations.deviceName = nix-on-droid.lib.nixOnDroidConfiguration { + nixOnDroidConfigurations.default = nix-on-droid.lib.nixOnDroidConfiguration { modules = [ ./nix-on-droid.nix ]; }; diff --git a/tests/on-device/config-flake-default.nix b/tests/on-device/config-flake-default.nix new file mode 100644 index 0000000..baa3302 --- /dev/null +++ b/tests/on-device/config-flake-default.nix @@ -0,0 +1,17 @@ +{ + description = "nix-on-droid configuration"; + + inputs = { + nixpkgs.url = "github:NixOS/nixpkgs/release-22.05"; + nix-on-droid.url = "<>"; + nix-on-droid.inputs.nixpkgs.follows = "nixpkgs"; + }; + + outputs = { nix-on-droid, ... }: { + nixOnDroidConfigurations = { + default = nix-on-droid.lib.nixOnDroidConfiguration { + modules = [ ./nix-on-droid.nix ]; + }; + }; + }; +} diff --git a/tests/on-device/config-flake.bats b/tests/on-device/config-flake.bats index 8664e2c..e3c9adf 100644 --- a/tests/on-device/config-flake.bats +++ b/tests/on-device/config-flake.bats @@ -1,8 +1,11 @@ -# Copyright (c) 2019-2021, see AUTHORS. Licensed under MIT License, see LICENSE. +# Copyright (c) 2019-2022, see AUTHORS. Licensed under MIT License, see LICENSE. load lib -@test 'flake example works' { +function flake_example() { + local flake_url="$1" + local flake_file_name="$2" + # assertions to verify initial state is as expected assert_command vi assert_no_command unzip @@ -15,10 +18,10 @@ load lib > ~/.config/nixpkgs/nix-on-droid.nix _sed "s|<>|$FLAKE_URL|g" \ - "$ON_DEVICE_TESTS_DIR/config-flake.nix" \ + "$ON_DEVICE_TESTS_DIR/$flake_file_name" \ > ~/.config/nixpkgs/flake.nix - nix-on-droid switch --flake ~/.config/nixpkgs#device + nix-on-droid switch --flake "$flake_url" # test presence of several crucial commands assert_command nix-on-droid nix-shell bash @@ -32,3 +35,11 @@ load lib assert_command vi assert_no_command unzip } + +@test 'flake example works' { + flake_example ~/.config/nixpkgs#device config-flake.nix +} + +@test 'flake with default config works' { + flake_example ~/.config/nixpkgs config-flake-default.nix +}