NixOs: use services on non-nixos OS, eventually with only user rightsMake systemd reload only single openvpn...

Does the Long March-11 increase its thrust after clearing the launch tower?

Is an entry level DSLR going to shoot nice portrait pictures?

How to ensure color fidelity of the same file on two computers?

A map of non-pathological topology?

Has there been a multiethnic Star Trek character?

A word that means "blending into a community too much"

Why does Sin[b-a] simplify to -Sin[a-b]?

Meaning of 'lose their grip on the groins of their followers'

Ability To Change Root User Password (Vulnerability?)

Does the new finding on "reversing a quantum jump mid-flight" rule out any interpretations of QM?

Is using 'echo' to display attacker-controlled data on the terminal dangerous?

Why am I getting a strange double quote (“) in Open Office instead of the ordinary one (")?

Is it a bad idea to to run 24 tap and shock lands in standard

Can I utilise a baking stone to make crepes?

Wooden cooking layout

Align equations within one column

Fermat's statement about the ancients: How serious was he?

Single-key teletype?

Why {1,2} printed by a command in $() is not interpolated?

LuaLaTex - how to use number, computed later in the document

Why does logistic function use e rather than 2?

Moving points closer to polyline using ModelBuilder?

Heap allocation on microcontroller

Bb13b9 confusion



NixOs: use services on non-nixos OS, eventually with only user rights


Make systemd reload only single openvpn process and not the whole groupsystemd and OpenVPN woes after an Ubuntu upgradesystemd unit files - conditional argumentsHow does Nix manage SystemD modules on a non-NixOS?systemd: finish the execution of custom shell script before starting nginxUse Systemd user services with ecryptfsSystemd Service stops on its user exit - debian 9 stretchHow to boot to single user mode in nixosConfiguring programs and services from unstable channel in NixOSDifferent methods to run a non-nixos executable on Nixos






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ margin-bottom:0;
}







0















I'd like to know how to be able to run a service described like in a module on a non-Nix OS. For example let's say I have the file:



{config, pkgs, ... }:
{
systemd.user.services.mytestservice = {
description = "Mytestservice";
script = "while true; do echo 'YES'; sleep 1; done";
wantedBy = [ "default.target" ];
};
}



(or eventually without the user: systemd.services.mytestservice =)



How can I compile it and run it on non-nixos OS, eventually by non-root users?










share|improve this question





























    0















    I'd like to know how to be able to run a service described like in a module on a non-Nix OS. For example let's say I have the file:



    {config, pkgs, ... }:
    {
    systemd.user.services.mytestservice = {
    description = "Mytestservice";
    script = "while true; do echo 'YES'; sleep 1; done";
    wantedBy = [ "default.target" ];
    };
    }



    (or eventually without the user: systemd.services.mytestservice =)



    How can I compile it and run it on non-nixos OS, eventually by non-root users?










    share|improve this question

























      0












      0








      0








      I'd like to know how to be able to run a service described like in a module on a non-Nix OS. For example let's say I have the file:



      {config, pkgs, ... }:
      {
      systemd.user.services.mytestservice = {
      description = "Mytestservice";
      script = "while true; do echo 'YES'; sleep 1; done";
      wantedBy = [ "default.target" ];
      };
      }



      (or eventually without the user: systemd.services.mytestservice =)



      How can I compile it and run it on non-nixos OS, eventually by non-root users?










      share|improve this question














      I'd like to know how to be able to run a service described like in a module on a non-Nix OS. For example let's say I have the file:



      {config, pkgs, ... }:
      {
      systemd.user.services.mytestservice = {
      description = "Mytestservice";
      script = "while true; do echo 'YES'; sleep 1; done";
      wantedBy = [ "default.target" ];
      };
      }



      (or eventually without the user: systemd.services.mytestservice =)



      How can I compile it and run it on non-nixos OS, eventually by non-root users?







      systemd nixos






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked 56 mins ago









      tobiasBoratobiasBora

      332312




      332312






















          1 Answer
          1






          active

          oldest

          votes


















          0














          Aknowledgment: Thanks a lot to clever for all the explainations!



          Do do that, let's first write the configuration file in myconfiguration.nix:



          {config, pkgs, ... }:

          {
          # You can actually remove the user, and still use it
          # as a user if you link it in ~/.config/systemd/user/
          # (do not forget to remove the `user` it in anything.nix
          # as well)
          systemd.user.services.mytestservice = {
          description = "Mytestservice";
          script = "while true; do echo 'YES'; sleep 1; done";
          # Or:
          # serviceConfig = {
          # ExecStart = "${pkgs.bash}/bin/bash -c "while true; do echo 'YES'; sleep 1; done"";
          # };
          wantedBy = [ "default.target" ];
          };
          }



          Then, you can de several things:




          • compile it

          • install it


          To compile only, you can do:



          nix-build '<nixpkgs/nixos>' -I nixos-config=myconfiguration.nix -A 'config.systemd.user.units."mytestservice.service".unit'


          The idea is that this loads the file default.nix in the folder /your/nixpkgs/copy/nixos/ (to get the path of nixpkgs, it checks the variable NIX_PATH that contains several "subkeys" like : NIX_PATH=nixpkgs=/your/nixpkgs/copy/:othervar=thepath), which is available online here. This file requires also <nixos-config>, to we use -I to add a nixos-config entry to the NIX_PATH environment variable. Then without the -A, it will try to build a full nixos, so we just specify that we want only this service unit.



          This will produce a file mytestservice.service that will look like that:



          $ cat result/mytestservice.service 
          [Unit]
          Description=Mytestservice

          [Service]
          Environment="LOCALE_ARCHIVE=/nix/store/zzhablipzgpv8mvlcvagqjnham6lr944-glibc-locales-2.27/lib/locale/locale-archive"
          Environment="PATH=/nix/store/bv1lw6a2kw0mn2y3lxhi43180idx6sp9-coreutils-8.31/bin:/nix/store/s1n4vl1f3in3nacalrc3xam0vyzpsfvs-findutils-4.6.0/bin:/nix/store/7d9bi31h40hky30f5scqx7r6wn311ain-gnugrep-3.3/bin:/nix/store/qg4qbkbca7qapfzpa8p991yjf944fc3w-gnused-4.7/bin:/nix/store/6bvd29jny80ka8df9prr5hrl5yz7d98k-systemd-239.20190219/bin:/nix/store/bv1lw6a2kw0mn2y3lxhi43180idx6sp9-coreutils-8.31/sbin:/nix/store/s1n4vl1f3in3nacalrc3xam0vyzpsfvs-findutils-4.6.0/sbin:/nix/store/7d9bi31h40hky30f5scqx7r6wn311ain-gnugrep-3.3/sbin:/nix/store/qg4qbkbca7qapfzpa8p991yjf944fc3w-gnused-4.7/sbin:/nix/store/6bvd29jny80ka8df9prr5hrl5yz7d98k-systemd-239.20190219/sbin"
          Environment="TZDIR=/nix/store/20wmykp8fj2izxdj8lic8ggcfpdid5ka-tzdata-2019a/share/zoneinfo"



          ExecStart=/nix/store/1f0wk7l4p7xv257dci8xxqz1k8nai9va-unit-script-mytestservice-start


          Now, if you want to be able to call it, you need to install it:



          nix-env -f '<nixpkgs/nixos>' -I nixos-config=myconfiguration.nix -iA 'config.systemd.user.units."mytestservice.service".unit'


          This will link the mytestservice.service into ~/.nix-profile/mytestservice.service. But systemctl expects it to be in ~/.config/systemd/user/, so we link it:



          ln -s ~/.nix-profile/mytestservice.service ~/.config/systemd/user/


          Then we need to reload the daemon, and we can try playing with it:



          systemctl --user daemon-reload
          systemctl --user start mytestservice.service


          Note however that the build/install commands are complicated and long to type, so we can create a file, say anything.nix, that will build everything for us:



          let
          eval = import <nixpkgs/nixos> {
          configuration = ./myconfiguration.nix;
          };
          pkgs = import <nixpkgs>{};
          in pkgs.buildEnv {
          name = "things";
          paths = [
          eval.config.systemd.user.units."mytestservice.service".unit
          ];
          }


          Now, you can compile with:



          nix-build anything.nix


          and install with



          nix-env -f anything.nix -i things


          Note that you may need to remove the first time the file we installed with the other method, by using something like:



          nix-env --query
          nix-env --uninstall unit-mytestservice.service


          Finally, both codes systemd.services and systemd.services.users seems to be usable with this method :D






          share|improve this answer
























            Your Answer








            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "106"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: false,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: null,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f523454%2fnixos-use-services-on-non-nixos-os-eventually-with-only-user-rights%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            0














            Aknowledgment: Thanks a lot to clever for all the explainations!



            Do do that, let's first write the configuration file in myconfiguration.nix:



            {config, pkgs, ... }:

            {
            # You can actually remove the user, and still use it
            # as a user if you link it in ~/.config/systemd/user/
            # (do not forget to remove the `user` it in anything.nix
            # as well)
            systemd.user.services.mytestservice = {
            description = "Mytestservice";
            script = "while true; do echo 'YES'; sleep 1; done";
            # Or:
            # serviceConfig = {
            # ExecStart = "${pkgs.bash}/bin/bash -c "while true; do echo 'YES'; sleep 1; done"";
            # };
            wantedBy = [ "default.target" ];
            };
            }



            Then, you can de several things:




            • compile it

            • install it


            To compile only, you can do:



            nix-build '<nixpkgs/nixos>' -I nixos-config=myconfiguration.nix -A 'config.systemd.user.units."mytestservice.service".unit'


            The idea is that this loads the file default.nix in the folder /your/nixpkgs/copy/nixos/ (to get the path of nixpkgs, it checks the variable NIX_PATH that contains several "subkeys" like : NIX_PATH=nixpkgs=/your/nixpkgs/copy/:othervar=thepath), which is available online here. This file requires also <nixos-config>, to we use -I to add a nixos-config entry to the NIX_PATH environment variable. Then without the -A, it will try to build a full nixos, so we just specify that we want only this service unit.



            This will produce a file mytestservice.service that will look like that:



            $ cat result/mytestservice.service 
            [Unit]
            Description=Mytestservice

            [Service]
            Environment="LOCALE_ARCHIVE=/nix/store/zzhablipzgpv8mvlcvagqjnham6lr944-glibc-locales-2.27/lib/locale/locale-archive"
            Environment="PATH=/nix/store/bv1lw6a2kw0mn2y3lxhi43180idx6sp9-coreutils-8.31/bin:/nix/store/s1n4vl1f3in3nacalrc3xam0vyzpsfvs-findutils-4.6.0/bin:/nix/store/7d9bi31h40hky30f5scqx7r6wn311ain-gnugrep-3.3/bin:/nix/store/qg4qbkbca7qapfzpa8p991yjf944fc3w-gnused-4.7/bin:/nix/store/6bvd29jny80ka8df9prr5hrl5yz7d98k-systemd-239.20190219/bin:/nix/store/bv1lw6a2kw0mn2y3lxhi43180idx6sp9-coreutils-8.31/sbin:/nix/store/s1n4vl1f3in3nacalrc3xam0vyzpsfvs-findutils-4.6.0/sbin:/nix/store/7d9bi31h40hky30f5scqx7r6wn311ain-gnugrep-3.3/sbin:/nix/store/qg4qbkbca7qapfzpa8p991yjf944fc3w-gnused-4.7/sbin:/nix/store/6bvd29jny80ka8df9prr5hrl5yz7d98k-systemd-239.20190219/sbin"
            Environment="TZDIR=/nix/store/20wmykp8fj2izxdj8lic8ggcfpdid5ka-tzdata-2019a/share/zoneinfo"



            ExecStart=/nix/store/1f0wk7l4p7xv257dci8xxqz1k8nai9va-unit-script-mytestservice-start


            Now, if you want to be able to call it, you need to install it:



            nix-env -f '<nixpkgs/nixos>' -I nixos-config=myconfiguration.nix -iA 'config.systemd.user.units."mytestservice.service".unit'


            This will link the mytestservice.service into ~/.nix-profile/mytestservice.service. But systemctl expects it to be in ~/.config/systemd/user/, so we link it:



            ln -s ~/.nix-profile/mytestservice.service ~/.config/systemd/user/


            Then we need to reload the daemon, and we can try playing with it:



            systemctl --user daemon-reload
            systemctl --user start mytestservice.service


            Note however that the build/install commands are complicated and long to type, so we can create a file, say anything.nix, that will build everything for us:



            let
            eval = import <nixpkgs/nixos> {
            configuration = ./myconfiguration.nix;
            };
            pkgs = import <nixpkgs>{};
            in pkgs.buildEnv {
            name = "things";
            paths = [
            eval.config.systemd.user.units."mytestservice.service".unit
            ];
            }


            Now, you can compile with:



            nix-build anything.nix


            and install with



            nix-env -f anything.nix -i things


            Note that you may need to remove the first time the file we installed with the other method, by using something like:



            nix-env --query
            nix-env --uninstall unit-mytestservice.service


            Finally, both codes systemd.services and systemd.services.users seems to be usable with this method :D






            share|improve this answer




























              0














              Aknowledgment: Thanks a lot to clever for all the explainations!



              Do do that, let's first write the configuration file in myconfiguration.nix:



              {config, pkgs, ... }:

              {
              # You can actually remove the user, and still use it
              # as a user if you link it in ~/.config/systemd/user/
              # (do not forget to remove the `user` it in anything.nix
              # as well)
              systemd.user.services.mytestservice = {
              description = "Mytestservice";
              script = "while true; do echo 'YES'; sleep 1; done";
              # Or:
              # serviceConfig = {
              # ExecStart = "${pkgs.bash}/bin/bash -c "while true; do echo 'YES'; sleep 1; done"";
              # };
              wantedBy = [ "default.target" ];
              };
              }



              Then, you can de several things:




              • compile it

              • install it


              To compile only, you can do:



              nix-build '<nixpkgs/nixos>' -I nixos-config=myconfiguration.nix -A 'config.systemd.user.units."mytestservice.service".unit'


              The idea is that this loads the file default.nix in the folder /your/nixpkgs/copy/nixos/ (to get the path of nixpkgs, it checks the variable NIX_PATH that contains several "subkeys" like : NIX_PATH=nixpkgs=/your/nixpkgs/copy/:othervar=thepath), which is available online here. This file requires also <nixos-config>, to we use -I to add a nixos-config entry to the NIX_PATH environment variable. Then without the -A, it will try to build a full nixos, so we just specify that we want only this service unit.



              This will produce a file mytestservice.service that will look like that:



              $ cat result/mytestservice.service 
              [Unit]
              Description=Mytestservice

              [Service]
              Environment="LOCALE_ARCHIVE=/nix/store/zzhablipzgpv8mvlcvagqjnham6lr944-glibc-locales-2.27/lib/locale/locale-archive"
              Environment="PATH=/nix/store/bv1lw6a2kw0mn2y3lxhi43180idx6sp9-coreutils-8.31/bin:/nix/store/s1n4vl1f3in3nacalrc3xam0vyzpsfvs-findutils-4.6.0/bin:/nix/store/7d9bi31h40hky30f5scqx7r6wn311ain-gnugrep-3.3/bin:/nix/store/qg4qbkbca7qapfzpa8p991yjf944fc3w-gnused-4.7/bin:/nix/store/6bvd29jny80ka8df9prr5hrl5yz7d98k-systemd-239.20190219/bin:/nix/store/bv1lw6a2kw0mn2y3lxhi43180idx6sp9-coreutils-8.31/sbin:/nix/store/s1n4vl1f3in3nacalrc3xam0vyzpsfvs-findutils-4.6.0/sbin:/nix/store/7d9bi31h40hky30f5scqx7r6wn311ain-gnugrep-3.3/sbin:/nix/store/qg4qbkbca7qapfzpa8p991yjf944fc3w-gnused-4.7/sbin:/nix/store/6bvd29jny80ka8df9prr5hrl5yz7d98k-systemd-239.20190219/sbin"
              Environment="TZDIR=/nix/store/20wmykp8fj2izxdj8lic8ggcfpdid5ka-tzdata-2019a/share/zoneinfo"



              ExecStart=/nix/store/1f0wk7l4p7xv257dci8xxqz1k8nai9va-unit-script-mytestservice-start


              Now, if you want to be able to call it, you need to install it:



              nix-env -f '<nixpkgs/nixos>' -I nixos-config=myconfiguration.nix -iA 'config.systemd.user.units."mytestservice.service".unit'


              This will link the mytestservice.service into ~/.nix-profile/mytestservice.service. But systemctl expects it to be in ~/.config/systemd/user/, so we link it:



              ln -s ~/.nix-profile/mytestservice.service ~/.config/systemd/user/


              Then we need to reload the daemon, and we can try playing with it:



              systemctl --user daemon-reload
              systemctl --user start mytestservice.service


              Note however that the build/install commands are complicated and long to type, so we can create a file, say anything.nix, that will build everything for us:



              let
              eval = import <nixpkgs/nixos> {
              configuration = ./myconfiguration.nix;
              };
              pkgs = import <nixpkgs>{};
              in pkgs.buildEnv {
              name = "things";
              paths = [
              eval.config.systemd.user.units."mytestservice.service".unit
              ];
              }


              Now, you can compile with:



              nix-build anything.nix


              and install with



              nix-env -f anything.nix -i things


              Note that you may need to remove the first time the file we installed with the other method, by using something like:



              nix-env --query
              nix-env --uninstall unit-mytestservice.service


              Finally, both codes systemd.services and systemd.services.users seems to be usable with this method :D






              share|improve this answer


























                0












                0








                0







                Aknowledgment: Thanks a lot to clever for all the explainations!



                Do do that, let's first write the configuration file in myconfiguration.nix:



                {config, pkgs, ... }:

                {
                # You can actually remove the user, and still use it
                # as a user if you link it in ~/.config/systemd/user/
                # (do not forget to remove the `user` it in anything.nix
                # as well)
                systemd.user.services.mytestservice = {
                description = "Mytestservice";
                script = "while true; do echo 'YES'; sleep 1; done";
                # Or:
                # serviceConfig = {
                # ExecStart = "${pkgs.bash}/bin/bash -c "while true; do echo 'YES'; sleep 1; done"";
                # };
                wantedBy = [ "default.target" ];
                };
                }



                Then, you can de several things:




                • compile it

                • install it


                To compile only, you can do:



                nix-build '<nixpkgs/nixos>' -I nixos-config=myconfiguration.nix -A 'config.systemd.user.units."mytestservice.service".unit'


                The idea is that this loads the file default.nix in the folder /your/nixpkgs/copy/nixos/ (to get the path of nixpkgs, it checks the variable NIX_PATH that contains several "subkeys" like : NIX_PATH=nixpkgs=/your/nixpkgs/copy/:othervar=thepath), which is available online here. This file requires also <nixos-config>, to we use -I to add a nixos-config entry to the NIX_PATH environment variable. Then without the -A, it will try to build a full nixos, so we just specify that we want only this service unit.



                This will produce a file mytestservice.service that will look like that:



                $ cat result/mytestservice.service 
                [Unit]
                Description=Mytestservice

                [Service]
                Environment="LOCALE_ARCHIVE=/nix/store/zzhablipzgpv8mvlcvagqjnham6lr944-glibc-locales-2.27/lib/locale/locale-archive"
                Environment="PATH=/nix/store/bv1lw6a2kw0mn2y3lxhi43180idx6sp9-coreutils-8.31/bin:/nix/store/s1n4vl1f3in3nacalrc3xam0vyzpsfvs-findutils-4.6.0/bin:/nix/store/7d9bi31h40hky30f5scqx7r6wn311ain-gnugrep-3.3/bin:/nix/store/qg4qbkbca7qapfzpa8p991yjf944fc3w-gnused-4.7/bin:/nix/store/6bvd29jny80ka8df9prr5hrl5yz7d98k-systemd-239.20190219/bin:/nix/store/bv1lw6a2kw0mn2y3lxhi43180idx6sp9-coreutils-8.31/sbin:/nix/store/s1n4vl1f3in3nacalrc3xam0vyzpsfvs-findutils-4.6.0/sbin:/nix/store/7d9bi31h40hky30f5scqx7r6wn311ain-gnugrep-3.3/sbin:/nix/store/qg4qbkbca7qapfzpa8p991yjf944fc3w-gnused-4.7/sbin:/nix/store/6bvd29jny80ka8df9prr5hrl5yz7d98k-systemd-239.20190219/sbin"
                Environment="TZDIR=/nix/store/20wmykp8fj2izxdj8lic8ggcfpdid5ka-tzdata-2019a/share/zoneinfo"



                ExecStart=/nix/store/1f0wk7l4p7xv257dci8xxqz1k8nai9va-unit-script-mytestservice-start


                Now, if you want to be able to call it, you need to install it:



                nix-env -f '<nixpkgs/nixos>' -I nixos-config=myconfiguration.nix -iA 'config.systemd.user.units."mytestservice.service".unit'


                This will link the mytestservice.service into ~/.nix-profile/mytestservice.service. But systemctl expects it to be in ~/.config/systemd/user/, so we link it:



                ln -s ~/.nix-profile/mytestservice.service ~/.config/systemd/user/


                Then we need to reload the daemon, and we can try playing with it:



                systemctl --user daemon-reload
                systemctl --user start mytestservice.service


                Note however that the build/install commands are complicated and long to type, so we can create a file, say anything.nix, that will build everything for us:



                let
                eval = import <nixpkgs/nixos> {
                configuration = ./myconfiguration.nix;
                };
                pkgs = import <nixpkgs>{};
                in pkgs.buildEnv {
                name = "things";
                paths = [
                eval.config.systemd.user.units."mytestservice.service".unit
                ];
                }


                Now, you can compile with:



                nix-build anything.nix


                and install with



                nix-env -f anything.nix -i things


                Note that you may need to remove the first time the file we installed with the other method, by using something like:



                nix-env --query
                nix-env --uninstall unit-mytestservice.service


                Finally, both codes systemd.services and systemd.services.users seems to be usable with this method :D






                share|improve this answer













                Aknowledgment: Thanks a lot to clever for all the explainations!



                Do do that, let's first write the configuration file in myconfiguration.nix:



                {config, pkgs, ... }:

                {
                # You can actually remove the user, and still use it
                # as a user if you link it in ~/.config/systemd/user/
                # (do not forget to remove the `user` it in anything.nix
                # as well)
                systemd.user.services.mytestservice = {
                description = "Mytestservice";
                script = "while true; do echo 'YES'; sleep 1; done";
                # Or:
                # serviceConfig = {
                # ExecStart = "${pkgs.bash}/bin/bash -c "while true; do echo 'YES'; sleep 1; done"";
                # };
                wantedBy = [ "default.target" ];
                };
                }



                Then, you can de several things:




                • compile it

                • install it


                To compile only, you can do:



                nix-build '<nixpkgs/nixos>' -I nixos-config=myconfiguration.nix -A 'config.systemd.user.units."mytestservice.service".unit'


                The idea is that this loads the file default.nix in the folder /your/nixpkgs/copy/nixos/ (to get the path of nixpkgs, it checks the variable NIX_PATH that contains several "subkeys" like : NIX_PATH=nixpkgs=/your/nixpkgs/copy/:othervar=thepath), which is available online here. This file requires also <nixos-config>, to we use -I to add a nixos-config entry to the NIX_PATH environment variable. Then without the -A, it will try to build a full nixos, so we just specify that we want only this service unit.



                This will produce a file mytestservice.service that will look like that:



                $ cat result/mytestservice.service 
                [Unit]
                Description=Mytestservice

                [Service]
                Environment="LOCALE_ARCHIVE=/nix/store/zzhablipzgpv8mvlcvagqjnham6lr944-glibc-locales-2.27/lib/locale/locale-archive"
                Environment="PATH=/nix/store/bv1lw6a2kw0mn2y3lxhi43180idx6sp9-coreutils-8.31/bin:/nix/store/s1n4vl1f3in3nacalrc3xam0vyzpsfvs-findutils-4.6.0/bin:/nix/store/7d9bi31h40hky30f5scqx7r6wn311ain-gnugrep-3.3/bin:/nix/store/qg4qbkbca7qapfzpa8p991yjf944fc3w-gnused-4.7/bin:/nix/store/6bvd29jny80ka8df9prr5hrl5yz7d98k-systemd-239.20190219/bin:/nix/store/bv1lw6a2kw0mn2y3lxhi43180idx6sp9-coreutils-8.31/sbin:/nix/store/s1n4vl1f3in3nacalrc3xam0vyzpsfvs-findutils-4.6.0/sbin:/nix/store/7d9bi31h40hky30f5scqx7r6wn311ain-gnugrep-3.3/sbin:/nix/store/qg4qbkbca7qapfzpa8p991yjf944fc3w-gnused-4.7/sbin:/nix/store/6bvd29jny80ka8df9prr5hrl5yz7d98k-systemd-239.20190219/sbin"
                Environment="TZDIR=/nix/store/20wmykp8fj2izxdj8lic8ggcfpdid5ka-tzdata-2019a/share/zoneinfo"



                ExecStart=/nix/store/1f0wk7l4p7xv257dci8xxqz1k8nai9va-unit-script-mytestservice-start


                Now, if you want to be able to call it, you need to install it:



                nix-env -f '<nixpkgs/nixos>' -I nixos-config=myconfiguration.nix -iA 'config.systemd.user.units."mytestservice.service".unit'


                This will link the mytestservice.service into ~/.nix-profile/mytestservice.service. But systemctl expects it to be in ~/.config/systemd/user/, so we link it:



                ln -s ~/.nix-profile/mytestservice.service ~/.config/systemd/user/


                Then we need to reload the daemon, and we can try playing with it:



                systemctl --user daemon-reload
                systemctl --user start mytestservice.service


                Note however that the build/install commands are complicated and long to type, so we can create a file, say anything.nix, that will build everything for us:



                let
                eval = import <nixpkgs/nixos> {
                configuration = ./myconfiguration.nix;
                };
                pkgs = import <nixpkgs>{};
                in pkgs.buildEnv {
                name = "things";
                paths = [
                eval.config.systemd.user.units."mytestservice.service".unit
                ];
                }


                Now, you can compile with:



                nix-build anything.nix


                and install with



                nix-env -f anything.nix -i things


                Note that you may need to remove the first time the file we installed with the other method, by using something like:



                nix-env --query
                nix-env --uninstall unit-mytestservice.service


                Finally, both codes systemd.services and systemd.services.users seems to be usable with this method :D







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered 56 mins ago









                tobiasBoratobiasBora

                332312




                332312






























                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Unix & Linux Stack Exchange!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f523454%2fnixos-use-services-on-non-nixos-os-eventually-with-only-user-rights%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Taj Mahal Inhaltsverzeichnis Aufbau | Geschichte | 350-Jahr-Feier | Heutige Bedeutung | Siehe auch |...

                    Baia Sprie Cuprins Etimologie | Istorie | Demografie | Politică și administrație | Arii naturale...

                    Nicolae Petrescu-Găină Cuprins Biografie | Opera | In memoriam | Varia | Controverse, incertitudini...