[Nix-dev] Re: optionally copying configuration.nix into system derivation

Marc Weber marco-oweber at gmx.de
Tue Apr 27 21:00:00 CEST 2010


Hi Ludovic Courtès,

> I’d prefer this option for 2 reasons:
> 
>   1. This particular use case might be common, so it makes sense to have
>      an option dedicated to it.

A true / false option is not enough for me (neither is it for Lluís
Batlle) [1]. So this means that we have to add two options:

a) copySystemConfiguration (which will add the cp line to extraSystemBuilderCmds)
b) extraSystemBuilderCmds

So we can add option a).

[1] What I really want to do is something like this:
  (cd /etc/nixos/nixos; git log -1) >> $out/nixos-version
  (cd /etc/nixos/nixpkgs; git log -1) >> $out/nixos-version
  cp $(builtins.getEnv "NIXOS_CONFIGA") $out/configurition.nix

>   2. It’s easy to shoot oneself in the foot with an option like
>      ‘extraSystemBuilderCmds’ because users don’t know exactly in which
>      context that command will be executed.
So I hope we can agree on adding two options one reusing the other (?)
This stills mean that you could shoot yourself in the foot - however
I don't understand what you exactly mean by this because
you should not be able to run rm -fr / because the build is run as usual
nix build using nix build user priviledges. So I don't see how this
makes it easier to shoot yourself in the foot than writing any other
derivation. Please explain what you had in mind when writing this.
I'll try to take care about it then if feasable.

About context: I still expect every Nix(OS) user being capable of using
grep like tools to see where an option is used. idutils is perfect and
fast for this kind of query.

Anyway: Here is the (much more verbose) comitt adding both options - is
it worth adding 4 lines to replace one custom line ? Judgement is up to
you.

  commit ac78f8935877f7a2aed194bf9398b54faa4952a1
  Author: Marc Weber <marco-oweber at gmx.de>
  Date:   Tue Apr 27 03:40:54 2010 +0200

      adding two new options:
      - system.copySystemConfiguration (requested by Ludovic Courtès)
      - system.extraSystemBuilderCmds

  diff --git a/lib/from-env.nix b/lib/from-env.nix
  index a7a339b..6bd71e4 100644
  --- a/lib/from-env.nix
  +++ b/lib/from-env.nix
  @@ -1,3 +1,4 @@
  +# TODO: remove this file. There is lib.maybeEnv now
   name: default:
   let value = builtins.getEnv name; in
   if value == "" then default else value
  diff --git a/modules/services/x11/xserver.nix b/modules/services/x11/xserver.nix
  index e408000..68bfed8 100644
  --- a/modules/services/x11/xserver.nix
  +++ b/modules/services/x11/xserver.nix
  @@ -155,7 +155,7 @@ in
   
         modules = mkOption {
           default = [];
  -        example = [ pkgs.linuxwacom ];
  +        example = [ pkgs.xorg.xf86inputsynaptics ];
           description = "Packages to be added to the module search path of the X server.";
         };
   
  diff --git a/modules/system/activation/top-level.nix b/modules/system/activation/top-level.nix
  index 0128ffb..6ae17f0 100644
  --- a/modules/system/activation/top-level.nix
  +++ b/modules/system/activation/top-level.nix
  @@ -31,6 +31,34 @@ let
           Name of the kernel file to be passed to the bootloader.
         '';
       };
  +
  +    system.copySystemConfiguration = pkgs.lib.mkOption {
  +      default = true;
  +      description = ''
  +        Unless set to false copies the nixos configuration file
  +        $NIXOS_CONFIG defaulting to /etc/nixos/configuration.nix
  +        to the system store path.
  +        See extraSystemBuilderCmds if you want to do add more customized info
  +        to your system storepath.
  +      '';
  +    };
  +
  +    system.extraSystemBuilderCmds = pkgs.lib.mkOption {
  +      default = "";
  +      merge = pkgs.lib.concatStringsSep "\n";
  +      description = ''
  +        This code will be added to the builder creating the system store path.
  +        This use case copies your configuration file into the system derivation:
  +        <command>
  +        cp ${pkgs.lib.maybeEnv "NIXOS_CONFIG" "/etc/nixos/configuration.nix"} $out
  +        </command>
  +        Of course you could add code saving a svn diff or svn revision number
  +        of both nixos and nixpkgs repositories as well. Keep in mind that when
  +        you build in chroots that you have do either copy sources to store or
  +        add them to the chroot somehow.
  +        You still should consider putting your configuration into a VCS.
  +      '';
  +    };
       
     };
   
  @@ -94,6 +122,8 @@ let
         ensureDir $out/bin
         substituteAll ${./switch-to-configuration.sh} $out/bin/switch-to-configuration
         chmod +x $out/bin/switch-to-configuration
  +
  +      ${config.system.extraSystemBuilderCmds}
       '';
   
     
  @@ -138,5 +168,9 @@ let
   in {
     require = [options];
   
  +  system.extraSystemBuilderCmds =
  +      pkgs.lib.optionalString
  +          config.system.copySystemConfiguration
  +          "cp ${pkgs.lib.maybeEnv "NIXOS_CONFIG" "/etc/nixos/configuration.nix"} $out";
     system.build.toplevel = system;
   }


If you still feel bad about this patch tell me why.

Marc Weber



More information about the nix-dev mailing list